2014-09-12 15:34:11 +08:00
|
|
|
#include <generated/csr.h>
|
|
|
|
#include <stdio.h>
|
2014-08-28 16:56:48 +08:00
|
|
|
|
2015-05-08 16:51:54 +08:00
|
|
|
#include "exceptions.h"
|
2014-10-14 12:47:04 +08:00
|
|
|
#include "rtio.h"
|
2015-06-21 08:42:39 +08:00
|
|
|
#include "log.h"
|
2014-08-28 16:56:48 +08:00
|
|
|
#include "dds.h"
|
|
|
|
|
2015-06-21 08:42:39 +08:00
|
|
|
#define DURATION_WRITE (5 << RTIO_FINE_TS_WIDTH)
|
2015-07-08 23:22:43 +08:00
|
|
|
|
|
|
|
#if defined DDS_AD9858
|
|
|
|
/* Assume 8-bit bus */
|
2015-05-09 17:26:36 +08:00
|
|
|
#define DURATION_INIT (7*DURATION_WRITE) /* not counting FUD */
|
|
|
|
#define DURATION_PROGRAM (8*DURATION_WRITE) /* not counting FUD */
|
2014-09-05 12:03:22 +08:00
|
|
|
|
2015-07-08 23:22:43 +08:00
|
|
|
#elif defined DDS_AD9914
|
|
|
|
/* Assume 16-bit bus */
|
2015-09-03 23:52:04 +08:00
|
|
|
/* DAC calibration takes max. 1ms as per datasheet */
|
|
|
|
#define DURATION_DAC_CAL (147000 << RTIO_FINE_TS_WIDTH)
|
2015-07-08 23:22:43 +08:00
|
|
|
/* not counting final FUD */
|
2015-08-27 10:47:44 +08:00
|
|
|
#define DURATION_INIT (10*DURATION_WRITE + DURATION_DAC_CAL)
|
2015-07-08 23:22:43 +08:00
|
|
|
#define DURATION_PROGRAM (5*DURATION_WRITE) /* not counting FUD */
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error Unknown DDS configuration
|
|
|
|
#endif
|
|
|
|
|
2015-05-08 14:44:39 +08:00
|
|
|
#define DDS_WRITE(addr, data) do { \
|
|
|
|
rtio_o_address_write(addr); \
|
|
|
|
rtio_o_data_write(data); \
|
|
|
|
rtio_o_timestamp_write(now); \
|
|
|
|
rtio_write_and_process_status(now, RTIO_DDS_CHANNEL); \
|
|
|
|
now += DURATION_WRITE; \
|
|
|
|
} while(0)
|
2014-08-28 16:56:48 +08:00
|
|
|
|
2015-05-09 17:11:34 +08:00
|
|
|
void dds_init_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
long long int now;
|
|
|
|
|
|
|
|
now = rtio_get_counter() + 10000;
|
|
|
|
for(i=0;i<DDS_CHANNEL_COUNT;i++) {
|
|
|
|
dds_init(now, i);
|
2015-05-09 17:26:36 +08:00
|
|
|
now += DURATION_INIT + DURATION_WRITE; /* + FUD time */
|
2015-05-09 17:11:34 +08:00
|
|
|
}
|
|
|
|
while(rtio_get_counter() < now);
|
|
|
|
}
|
|
|
|
|
2015-05-08 14:44:39 +08:00
|
|
|
void dds_init(long long int timestamp, int channel)
|
2014-08-28 16:56:48 +08:00
|
|
|
{
|
2015-05-08 14:44:39 +08:00
|
|
|
long long int now;
|
|
|
|
|
|
|
|
rtio_chan_sel_write(RTIO_DDS_CHANNEL);
|
|
|
|
|
2015-05-09 17:11:34 +08:00
|
|
|
now = timestamp - DURATION_INIT;
|
2015-05-08 14:44:39 +08:00
|
|
|
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_ONEHOT_SEL
|
|
|
|
channel = 1 << channel;
|
|
|
|
#endif
|
|
|
|
channel <<= 1;
|
2015-05-08 14:44:39 +08:00
|
|
|
DDS_WRITE(DDS_GPIO, channel);
|
2015-07-08 23:22:43 +08:00
|
|
|
DDS_WRITE(DDS_GPIO, channel | 1); /* reset */
|
2015-05-08 14:44:39 +08:00
|
|
|
DDS_WRITE(DDS_GPIO, channel);
|
|
|
|
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_AD9858
|
|
|
|
/*
|
|
|
|
* 2GHz divider disable
|
|
|
|
* SYNCLK disable
|
|
|
|
* Mixer power-down
|
|
|
|
* Phase detect power down
|
|
|
|
*/
|
|
|
|
DDS_WRITE(DDS_CFR0, 0x78);
|
|
|
|
DDS_WRITE(DDS_CFR1, 0x00);
|
|
|
|
DDS_WRITE(DDS_CFR2, 0x00);
|
|
|
|
DDS_WRITE(DDS_CFR3, 0x00);
|
|
|
|
DDS_WRITE(DDS_FUD, 0);
|
|
|
|
#endif
|
2015-05-09 17:11:34 +08:00
|
|
|
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_AD9914
|
2015-08-22 12:25:44 +08:00
|
|
|
DDS_WRITE(DDS_CFR1H, 0x0000); /* Enable cosine output */
|
|
|
|
DDS_WRITE(DDS_CFR2L, 0x8900); /* Enable matched latency */
|
2015-08-27 10:47:44 +08:00
|
|
|
DDS_WRITE(DDS_CFR2H, 0x0080); /* Enable profile mode */
|
|
|
|
DDS_WRITE(DDS_ASF, 0x0fff); /* Set amplitude to maximum */
|
2015-08-22 12:25:44 +08:00
|
|
|
DDS_WRITE(DDS_CFR4H, 0x0105); /* Enable DAC calibration */
|
2015-05-09 17:11:34 +08:00
|
|
|
DDS_WRITE(DDS_FUD, 0);
|
2015-07-08 23:22:43 +08:00
|
|
|
now += DURATION_DAC_CAL;
|
2015-08-22 12:25:44 +08:00
|
|
|
DDS_WRITE(DDS_CFR4H, 0x0005); /* Disable DAC calibration */
|
2015-07-08 23:22:43 +08:00
|
|
|
DDS_WRITE(DDS_FUD, 0);
|
|
|
|
#endif
|
2014-11-21 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2015-06-21 08:42:39 +08:00
|
|
|
/* Compensation to keep phase continuity when switching from absolute or tracking
|
|
|
|
* to continuous phase mode. */
|
|
|
|
static unsigned int continuous_phase_comp[DDS_CHANNEL_COUNT];
|
|
|
|
|
|
|
|
static void dds_set_one(long long int now, long long int ref_time, unsigned int channel,
|
2015-05-08 14:44:39 +08:00
|
|
|
unsigned int ftw, unsigned int pow, int phase_mode)
|
2014-11-21 04:32:56 +08:00
|
|
|
{
|
2015-07-08 23:22:43 +08:00
|
|
|
unsigned int channel_enc;
|
|
|
|
|
2015-06-21 08:42:39 +08:00
|
|
|
if(channel >= DDS_CHANNEL_COUNT) {
|
|
|
|
log("Attempted to set invalid DDS channel");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_ONEHOT_SEL
|
|
|
|
channel_enc = 1 << channel;
|
|
|
|
#else
|
|
|
|
channel_enc = channel;
|
|
|
|
#endif
|
|
|
|
DDS_WRITE(DDS_GPIO, channel_enc << 1);
|
|
|
|
|
|
|
|
#ifdef DDS_AD9858
|
2014-09-05 12:03:22 +08:00
|
|
|
DDS_WRITE(DDS_FTW0, ftw & 0xff);
|
|
|
|
DDS_WRITE(DDS_FTW1, (ftw >> 8) & 0xff);
|
|
|
|
DDS_WRITE(DDS_FTW2, (ftw >> 16) & 0xff);
|
|
|
|
DDS_WRITE(DDS_FTW3, (ftw >> 24) & 0xff);
|
2015-07-08 23:22:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DDS_AD9914
|
|
|
|
DDS_WRITE(DDS_FTWL, ftw & 0xffff);
|
|
|
|
DDS_WRITE(DDS_FTWH, (ftw >> 16) & 0xffff);
|
|
|
|
#endif
|
2014-11-21 04:32:56 +08:00
|
|
|
|
2015-06-20 01:01:43 +08:00
|
|
|
/* We need the RTIO fine timestamp clock to be phase-locked
|
2015-06-20 05:30:17 +08:00
|
|
|
* to DDS SYSCLK, and divided by an integer DDS_RTIO_CLK_RATIO.
|
2015-06-20 01:01:43 +08:00
|
|
|
*/
|
|
|
|
if(phase_mode == PHASE_MODE_CONTINUOUS) {
|
|
|
|
/* Do not clear phase accumulator on FUD */
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_AD9858
|
|
|
|
DDS_WRITE(DDS_CFR2, 0x00);
|
|
|
|
#endif
|
|
|
|
#ifdef DDS_AD9914
|
|
|
|
DDS_WRITE(DDS_CFR1L, 0x0008);
|
|
|
|
#endif
|
2015-06-21 08:42:39 +08:00
|
|
|
pow += continuous_phase_comp[channel];
|
2015-06-20 01:01:43 +08:00
|
|
|
} else {
|
2015-05-09 17:26:36 +08:00
|
|
|
long long int fud_time;
|
|
|
|
|
2015-06-20 01:01:43 +08:00
|
|
|
/* Clear phase accumulator on FUD */
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_AD9858
|
|
|
|
DDS_WRITE(DDS_CFR2, 0x40);
|
|
|
|
#endif
|
|
|
|
#ifdef DDS_AD9914
|
|
|
|
DDS_WRITE(DDS_CFR1L, 0x2008);
|
|
|
|
#endif
|
2015-05-09 17:26:36 +08:00
|
|
|
fud_time = now + 2*DURATION_WRITE;
|
2015-07-08 23:22:43 +08:00
|
|
|
pow -= (ref_time - fud_time)*DDS_RTIO_CLK_RATIO*ftw >> (32-DDS_POW_WIDTH);
|
2015-06-20 01:01:43 +08:00
|
|
|
if(phase_mode == PHASE_MODE_TRACKING)
|
2015-07-08 23:22:43 +08:00
|
|
|
pow += ref_time*DDS_RTIO_CLK_RATIO*ftw >> (32-DDS_POW_WIDTH);
|
2015-06-21 08:42:39 +08:00
|
|
|
continuous_phase_comp[channel] = pow;
|
2015-05-09 17:26:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 23:22:43 +08:00
|
|
|
#ifdef DDS_AD9858
|
2014-11-21 04:32:56 +08:00
|
|
|
DDS_WRITE(DDS_POW0, pow & 0xff);
|
|
|
|
DDS_WRITE(DDS_POW1, (pow >> 8) & 0x3f);
|
2015-07-08 23:22:43 +08:00
|
|
|
#endif
|
|
|
|
#ifdef DDS_AD9914
|
|
|
|
DDS_WRITE(DDS_POW, pow);
|
|
|
|
#endif
|
2015-05-09 17:26:36 +08:00
|
|
|
DDS_WRITE(DDS_FUD, 0);
|
2015-05-08 14:44:39 +08:00
|
|
|
}
|
|
|
|
|
2015-05-08 16:51:54 +08:00
|
|
|
struct dds_set_params {
|
|
|
|
int channel;
|
|
|
|
unsigned int ftw;
|
|
|
|
unsigned int pow;
|
|
|
|
int phase_mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int batch_mode;
|
|
|
|
static int batch_count;
|
2015-05-09 17:26:36 +08:00
|
|
|
static long long int batch_ref_time;
|
2015-05-08 16:51:54 +08:00
|
|
|
static struct dds_set_params batch[DDS_MAX_BATCH];
|
|
|
|
|
|
|
|
void dds_batch_enter(long long int timestamp)
|
|
|
|
{
|
|
|
|
if(batch_mode)
|
|
|
|
exception_raise(EID_DDS_BATCH_ERROR);
|
|
|
|
batch_mode = 1;
|
|
|
|
batch_count = 0;
|
2015-05-09 17:26:36 +08:00
|
|
|
batch_ref_time = timestamp;
|
2015-05-08 16:51:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void dds_batch_exit(void)
|
2015-05-08 14:44:39 +08:00
|
|
|
{
|
|
|
|
long long int now;
|
2015-05-08 16:51:54 +08:00
|
|
|
int i;
|
2014-11-21 04:32:56 +08:00
|
|
|
|
2015-05-08 16:51:54 +08:00
|
|
|
if(!batch_mode)
|
|
|
|
exception_raise(EID_DDS_BATCH_ERROR);
|
2015-05-08 22:09:08 +08:00
|
|
|
rtio_chan_sel_write(RTIO_DDS_CHANNEL);
|
2015-05-09 17:26:36 +08:00
|
|
|
/* + FUD time */
|
|
|
|
now = batch_ref_time - batch_count*(DURATION_PROGRAM + DURATION_WRITE);
|
2015-05-08 16:51:54 +08:00
|
|
|
for(i=0;i<batch_count;i++) {
|
2015-05-09 17:26:36 +08:00
|
|
|
dds_set_one(now, batch_ref_time,
|
2015-05-08 16:51:54 +08:00
|
|
|
batch[i].channel, batch[i].ftw, batch[i].pow, batch[i].phase_mode);
|
2015-05-09 17:26:36 +08:00
|
|
|
now += DURATION_PROGRAM + DURATION_WRITE;
|
2015-05-08 16:51:54 +08:00
|
|
|
}
|
|
|
|
batch_mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dds_set(long long int timestamp, int channel,
|
|
|
|
unsigned int ftw, unsigned int pow, int phase_mode)
|
|
|
|
{
|
|
|
|
if(batch_mode) {
|
|
|
|
if(batch_count >= DDS_MAX_BATCH)
|
|
|
|
exception_raise(EID_DDS_BATCH_ERROR);
|
|
|
|
/* timestamp parameter ignored (determined by batch) */
|
|
|
|
batch[batch_count].channel = channel;
|
|
|
|
batch[batch_count].ftw = ftw;
|
|
|
|
batch[batch_count].pow = pow;
|
|
|
|
batch[batch_count].phase_mode = phase_mode;
|
|
|
|
batch_count++;
|
|
|
|
} else {
|
|
|
|
rtio_chan_sel_write(RTIO_DDS_CHANNEL);
|
|
|
|
dds_set_one(timestamp - DURATION_PROGRAM, timestamp, channel, ftw, pow, phase_mode);
|
|
|
|
}
|
2014-08-28 16:56:48 +08:00
|
|
|
}
|