/*
* LMS6002.c
*
* Created on: 31.10.2011
* Author: kovalev
*/
#include "precomp.h"
#define verbosity 0
// Constants used during PLL tuning.
#define VCO_HIGH 0x02
#define VCO_NORM 0x00
#define VCO_LOW 0x01
/*
* XSpi instances of first and second Lime transceivers
*/
//XSpi lime_spi_1;
//XSpi lime_spi_2;
extern __declspec(dllexport) UINT32 lime_write(HANDLE instrument_handle, u8 addr, u8 value)
{
UINT32 result = STATUS_SUCCESS;
IOCTL_DATA ioctrl_in, ioctrl_out;
BOOL bRc;
ULONG bytesReturned;
FillMemory(&ioctrl_in, sizeof(IOCTL_DATA), 0);
FillMemory(&ioctrl_out, sizeof(IOCTL_DATA), 0);
ioctrl_in.spi_no = 0; // 0 or 1
ioctrl_in.cs_num = 0; // LMS6002D
ioctrl_in.spi_div = 0x080010;
ioctrl_in.need_ans = 0;
ioctrl_in.spi_data = (addr | 0x80) * 0x100 + value;
bRc = DeviceIoControl (instrument_handle,
(DWORD) IOCTL_SIOCTL_METHOD_BUFFERED_SPI_TRANSMIT,
&ioctrl_in, sizeof(IOCTL_DATA), NULL, 0, &bytesReturned, NULL);
if ( !bRc )
{
result = STATUS_UNSUCCESSFUL;
goto exit;
}
exit:
return result;
}
extern __declspec(dllexport) UINT32 lime_read(HANDLE instrument_handle, u8 addr, u8 *value)
{
UINT32 result = STATUS_SUCCESS;
IOCTL_DATA ioctrl_in, ioctrl_out;
BOOL bRc;
ULONG bytesReturned;
FillMemory(&ioctrl_in, sizeof(IOCTL_DATA), 0);
FillMemory(&ioctrl_out, sizeof(IOCTL_DATA), 0);
ioctrl_in.spi_no = 0; // 0 or 1
ioctrl_in.cs_num = 0; // LMS6002D
ioctrl_in.spi_div = 0x080010;
ioctrl_in.need_ans = 1;
ioctrl_in.spi_data = addr * 0x100;
bRc = DeviceIoControl (instrument_handle,
(DWORD) IOCTL_SIOCTL_METHOD_BUFFERED_SPI_TRANSMIT,
&ioctrl_in, sizeof(IOCTL_DATA), &ioctrl_out, sizeof(IOCTL_DATA), &bytesReturned, NULL);
*value = (u8)ioctrl_out.spi_data;
if ( !bRc )
{
result = STATUS_UNSUCCESSFUL;
goto exit;
}
exit:
return result;
}
UINT32 lms_set_bits(HANDLE instrument_handle, u8 address, u8 mask, u8 *old)
{
UINT32 result = STATUS_SUCCESS;
u8 data;
result = lime_read(instrument_handle, address, &data);
if (!NT_SUCCESS(result))
{
goto exit;
}
*old = data;
result = lime_write(instrument_handle, address, data | mask);
if (!NT_SUCCESS(result))
{
goto exit;
}
exit:
return result;
}
UINT32 lms_clear_bits(HANDLE instrument_handle, u8 address, u8 mask, u8 *old)
{
UINT32 result = STATUS_SUCCESS;
u8 data;
result = lime_read(instrument_handle, address, &data);
if (!NT_SUCCESS(result))
{
goto exit;
}
*old = data;
result = lime_write(instrument_handle, address, data & (~mask));
if (!NT_SUCCESS(result))
{
goto exit;
}
exit:
return result;
}
UINT32 lms_write_bits(HANDLE instrument_handle, u8 address, u8 mask, u8 bits, u8 *reg)
{
UINT32 result = STATUS_SUCCESS;
result = lime_read(instrument_handle, address, reg);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, address, (*reg & (~mask)) | bits);
if (!NT_SUCCESS(result))
{
goto exit;
}
exit:
return result;
}
UINT32 lms_read_shift(HANDLE instrument_handle, u8 address, u8 mask, u8 shift, u8 *reg)
{
UINT32 result = STATUS_SUCCESS;
u8 data;
result = lime_read(instrument_handle, address, &data);
if (!NT_SUCCESS(result))
{
goto exit;
}
*reg = (data & mask) >> shift;
exit:
return result;
}
extern __declspec(dllexport) UINT32 init(HANDLE instrument_handle)
{
UINT32 result = STATUS_SUCCESS;
result = lime_write(instrument_handle, 0x09, 0x00); // RXOUTSW (disabled), CLK_EN (all disabled)
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x17, 0xE0);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x27, 0xE3);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x64, 0x32);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x70, 0x01);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x79, 0x37);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x59, 0x09);
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x47, 0x40);
if (!NT_SUCCESS(result))
{
goto exit;
}
// RF Settings
result = lime_write(instrument_handle, 0x41, 0x15); // VGA1GAIN
if (!NT_SUCCESS(result))
{
goto exit;
}
result = lime_write(instrument_handle, 0x45, 0x00); // VGA2GAIN, ENVD
if (!NT_SUCCESS(result))
{
goto exit;
}
//reg_dump();
exit:
return result;
}
/* Dump all registers 128 bytes*/
extern __declspec(dllexport) UINT32 dump(HANDLE instrument_handle, UCHAR *dump)
{
UINT32 result = STATUS_SUCCESS;
int i;
for (i = 0; i < 128; i++)
{
switch (i)
{
case 0x0C:
case 0x0D:
case 0x37:
case 0x38:
case 0x39:
case 0x3A:
case 0x3B:
case 0x3C:
case 0x3D:
case 0x69:
case 0x6A:
case 0x6B:
case 0x6C:
case 0x6D:
continue;
}
result = lime_read(instrument_handle,(u8)i, &dump[i]);
if (!NT_SUCCESS(result))
{
goto exit;
}
printf("i=%x LMS=%x\n", i, &dump[i]);
}
exit:
return result;
}
static void usleep(int us)
{
volatile int i;
for (i = 0; i < us * 100; i++)
;
}
UINT32 lms_txrx_pll_tune(HANDLE instrument_handle, u8 reg, float ref_clock, float out_freq)
{
UINT32 result = STATUS_SUCCESS;
// Supported frequency ranges and corresponding FREQSEL values.
static const struct vco_sel { float fmin; float fmax; u8 value; } freqsel[] = {
{ 0.2325e9f, 0.285625e9f, 0x27 },
{ 0.285625e9f, 0.336875e9f, 0x2f },
{ 0.336875e9f, 0.405e9f, 0x37 },
{ 0.405e9f, 0.465e9f, 0x3f },
{ 0.465e9f, 0.57125e9f, 0x26 },
{ 0.57125e9f, 0.67375e9f, 0x2e },
{ 0.67375e9f, 0.81e9f, 0x36 },
{ 0.81e9f, 0.93e9f, 0x3e },
{ 0.93e9f, 1.1425e9f, 0x25 },
{ 1.1425e9f, 1.3475e9f, 0x2d },
{ 1.3475e9f, 1.62e9f, 0x35 },
{ 1.62e9f, 1.86e9f, 0x3d },
{ 1.86e9f, 2.285e9f, 0x24 },
{ 2.285e9f, 2.695e9f, 0x2c },
{ 2.695e9f, 3.24e9f, 0x34 },
{ 3.24e9f, 3.72e9f, 0x3c },
};
unsigned int i;
int start_i = -1;
int stop_i = -1;
u8 state = VCO_HIGH;
ULONG64 vco_x;
ULONG64 nint;
ULONG64 nfrack;
u8 found_freqsel = 0;
u8 comp;
u8 b;
int avg_i;
// Find frequency range and FREQSEL for the given frequency
for (i = 0; i < (int)sizeof(freqsel) / sizeof(freqsel[0]); i++) {
if (out_freq > freqsel[i].fmin && out_freq <= freqsel[i].fmax) {
found_freqsel = freqsel[i].value; break;
}
}
if (found_freqsel == -1)
{
// Unsupported frequency range
result = STATUS_UNSUCCESSFUL;
goto exit;
}
// Calculate NINT, NFRAC
vco_x = (ULONG64)(1 << ((found_freqsel & 0x7) - 3));
nint = (ULONG64)(vco_x * out_freq / ref_clock);
nfrack = (ULONG64)((1 << 23) * (vco_x * out_freq - nint * ref_clock) / ref_clock);
// Write NINT, NFRAC
result = lime_write(instrument_handle, reg + 0x0, (nint >> 1) & 0xff); // NINT[8:1]
result = lime_write(instrument_handle, reg + 0x1, ((nfrack >> 16) & 0x7f) | ((nint & 0x1) << 7)); //NINT[0] NFRACK[22:16]
result = lime_write(instrument_handle, reg + 0x2, (nfrack >> 8) & 0xff); // NFRACK[15:8]
result = lime_write(instrument_handle, reg + 0x3, (nfrack) & 0xff); // NFRACK[7:0]
// Write FREQSEL
result = lms_write_bits(instrument_handle, reg + 0x5,
lms.zip_LMS6002D_lms python_single
版权申诉
159 浏览量
2022-09-20
19:48:29
上传
评论
收藏 19KB ZIP 举报
局外狗
- 粉丝: 66
- 资源: 1万+