92 lines
2.0 KiB
C
92 lines
2.0 KiB
C
|
|
#include "dds.h"
|
||
|
|
#include "io.h"
|
||
|
|
|
||
|
|
static void sendCommand(dds_t *dds, uint16_t reg, uint8_t *data, uint16_t len)
|
||
|
|
{
|
||
|
|
uint8_t tmp[2];
|
||
|
|
tmp[0] = (uint8_t)(reg >> 8); // Reset bit held
|
||
|
|
tmp[1] = (uint8_t)(reg & 0x00ff);
|
||
|
|
|
||
|
|
SPI0_SendBytes(tmp, 2, dds->mode);
|
||
|
|
|
||
|
|
if (data != NULL)
|
||
|
|
{
|
||
|
|
SPI0_SendBytes(data, len, dds->mode);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static uint32_t calculateDDSValue(uint32_t frequency) // Calculate frequency word for DDS
|
||
|
|
{
|
||
|
|
// FREG = (Frequency * 2^28)/fclk = 12MHz normally
|
||
|
|
return (uint32_t)((frequency * 268435456.0) / DDS_CLK + 0.5);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void dds_setFrequency(dds_t *dds, uint32_t frequency)
|
||
|
|
{
|
||
|
|
uint16_t freq_hi = FREQ0_REG;
|
||
|
|
uint16_t freq_lo = FREQ0_REG;
|
||
|
|
|
||
|
|
dds->frequency = frequency;
|
||
|
|
|
||
|
|
uint8_t data[4];
|
||
|
|
|
||
|
|
// set signal DDS
|
||
|
|
if (frequency > 0)
|
||
|
|
{
|
||
|
|
uint32_t freqValue = calculateDDSValue(dds->frequency);
|
||
|
|
|
||
|
|
freq_hi = FREQ0_REG;
|
||
|
|
freq_lo = FREQ0_REG;
|
||
|
|
|
||
|
|
freq_lo |= (freqValue & 0x3fff);
|
||
|
|
freqValue >>= 14;
|
||
|
|
freq_hi |= (freqValue & 0x3fff);
|
||
|
|
|
||
|
|
data[0] = (uint8_t)(freq_lo >> 8); //LS 16-bit word, MSB
|
||
|
|
data[1] = (uint8_t)(freq_lo & 0x00ff); //LS 16-bit word, LSB
|
||
|
|
data[2] = (uint8_t)(freq_hi >> 8);
|
||
|
|
data[3] = (uint8_t)(freq_hi & 0x00ff);
|
||
|
|
|
||
|
|
// send Control word to hold setup
|
||
|
|
sendCommand(dds, FRQ_CTRL_WORD1, data, sizeof(data));
|
||
|
|
|
||
|
|
// send Control word to release setup
|
||
|
|
sendCommand(dds, FRQ_CTRL_WORD2, NULL, 0);
|
||
|
|
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
dds_sleep(dds, true, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void dds_init(dds_t *dds, SPI_MODE_t mode, gpio_pin_t resetPin)
|
||
|
|
{
|
||
|
|
dds->mode = mode;
|
||
|
|
dds->resetPin = resetPin;
|
||
|
|
|
||
|
|
dds_reset(dds, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
void dds_reset(dds_t *dds, bool reset)
|
||
|
|
{
|
||
|
|
GPIO_PinWrite(GPIO, dds->resetPin.port, dds->resetPin.pin, (reset ? 1 : 0) );
|
||
|
|
}
|
||
|
|
|
||
|
|
void dds_sleep(dds_t *dds, bool sleep, bool disableDAC)
|
||
|
|
{
|
||
|
|
if (sleep)
|
||
|
|
{
|
||
|
|
if (disableDAC)
|
||
|
|
{
|
||
|
|
sendCommand(dds, SLP_CTRL_WRD2, NULL, 0);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
sendCommand(dds, SLP_CTRL_WRD, NULL, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|