-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad9911.c
259 lines (210 loc) · 6.94 KB
/
ad9911.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
*
* Library for Analog Devices AD9911
* Copyright (C) 2013 Patrick Rudolph <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*
* This code was written for Teensy 2.0 (ATMEGA32U4).
*/
#include <stdio.h>
#include <inttypes.h>
#include <avr/io.h>
#include "ad9911.h"
#define AD9911_RWb 0x80
#define AD9911_addressmask 0x1f
#define AD9911_MAXREG 0x18
#define AD9911_ampmask 0x3ff
/* hardcode samplerate here 500.000.000Mhz */
#define fs 500000000UL
enum AD9911_addr_e{
CSR = 0,
FR1,
FR2,
CFR,
CTW0,
CPOW0,
ACR,
LSR,
RDW,
FDW,
CTW1,
CTW2,
CTW3,
CTW4,
CTW5,
CTW6,
CTW7,
CTW8,
CTW9,
CTW10,
CTW11,
CTW12,
CTW13,
CTW14,
CTW15,
};
uint32_t AD9911_reg[AD9911_MAXREG + 1];
/* these routines are specific for ATMEGA32 */
void WriteByteSPI(unsigned char byte)
{
SPDR = byte; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
}
unsigned char ReadByteSPI(unsigned char addr)
{
SPDR = addr; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
addr=SPDR;
return addr;
}
/* in SPI write mode CPOL=0 CPHA=0, write msb bit first, write msb byte first */
void _AD9911_write( uint8_t addr, uint32_t data )
{
uint8_t AD9911_cmd, len;
int i;
if( addr > AD9911_MAXREG )
return;
// SPI CPOL=0 CPHA=0
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0)|(0<<CPOL)|(0<<CPHA);
PORTB &= ~(1<<0); // ss low
AD9911_cmd = addr & AD9911_addressmask;
if( addr > 7 || addr == 4 )
len = 4;
else if( addr == 7 || addr == 5 || addr == 2 )
len = 2;
else if( addr == 0 )
len = 1;
else
len = 3;
WriteByteSPI( AD9911_cmd );
for(i=len-1;i >= 0;i--)
{
WriteByteSPI( (uint8_t)((data>>(8*i)) & 0xff) );
}
PORTB |= (1<<0); // ss high
}
/* in SPI read mode CPOL=1 CPHA=1, read msb bit first, read msb byte first */
void _AD9911_read( uint8_t addr, uint32_t *data )
{
uint8_t AD9911_cmd, len;
int i;
if( addr > AD9911_MAXREG )
return;
// SPI CPOL=1 CPHA=1
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(1<<CPHA);
PORTB &= ~(1<<0); // ss low
AD9911_cmd = addr & AD9911_addressmask;
AD9911_cmd |= AD9911_RWb;
if( addr > 7 || addr == 4 )
len = 4;
else if( addr == 7 || addr == 5 || addr == 2 )
len = 2;
else if( addr == 0 )
len = 1;
else
len = 3;
WriteByteSPI( AD9911_cmd );
*data = 0;
for(i=len-1;i >= 0;i--)
{
*data |= ReadByteSPI(0)<<(8*i);
}
PORTB |= (1<<0); // ss high
}
/* rising edge on IOUpdate pin loads the contents of the shift register */
void _AD9911_IOupdate(void)
{
int i;
/* start at FR1 */
for(i=1;i<AD9911_MAXREG;i++)
_AD9911_write(i, AD9911_reg[i]);
PORTD |= 2; // IO UPDATE
PORTD &= ~2;
}
void _AD9911_select_channel(uint8_t channel)
{
/* select channel to write to */
if( channel > 3 )
return;
_AD9911_write(CSR, (AD9911_reg[CSR] & 0x0f) | (1<<(channel+4)) );
}
/* set constant values and disable all channels */
void AD9911_init()
{
int i;
DDRB = 0x7; // SS, SCLK, MOSI
DDRD = 0x3;
PORTD = (1<<0); // master reset high
for(i=0;i<0xff;i++);
PORTD = 0; // master reset low, IOUpdate low
for(i=0;i<AD9911_MAXREG;i++)
AD9911_reg[i] = 0;
/* MSB first , 3-wire SPI mode, enable all channels */
AD9911_reg[CSR] = ((uint32_t)0<<0) | ((uint32_t)1 << 1) |((uint32_t)0x0f<<4);
_AD9911_write(CSR, AD9911_reg[CSR]);
/* PLL 20x, vco gain high range */
AD9911_reg[FR1] = ((uint32_t)20<<18) | ((uint32_t)1<<23);
_AD9911_write(FR1, AD9911_reg[FR1]);
/* full scale dac output, digital power down */
AD9911_reg[CFR] = ((uint32_t)3<<8)|((uint32_t)1<<7);
_AD9911_write(CFR, AD9911_reg[CFR]);
_AD9911_select_channel( 1 ); /* all registers are written to channel 1 on IOUpdate */
}
uint32_t _AD9911_FTW_from_freq( uint32_t fo )
{
uint64_t tmp;
if( fo > fs/2 )
return 0;
tmp = (uint64_t)fo << 32;
tmp /= fs;
return tmp;
}
void AD9911_single_tone( uint32_t freq, uint16_t phase, uint16_t amp )
{
_AD9911_select_channel( 1 ); /* all registers are written to channel 1 on IOUpdate */
AD9911_reg[FR1] &= ~((uint32_t)1<<2); /* disable test-tone mode */
AD9911_reg[CFR] &= ~((uint32_t)1<<7); /* disable digital power down */
AD9911_reg[CTW0] = _AD9911_FTW_from_freq( freq );
AD9911_reg[CPOW0] = (uint32_t)phase;
AD9911_reg[ACR] = (uint32_t)amp & AD9911_ampmask;
_AD9911_IOupdate();
}
/* untested */
void AD9911_test_tone( uint32_t freq, uint16_t freq2, uint16_t amp )
{
_AD9911_select_channel( 1 ); /* all registers are written to channel 1 on IOUpdate */
AD9911_reg[CFR] &= ~((uint32_t)1<<7); /* disable digital power down */
AD9911_reg[FR1] |= ((uint32_t)1<<2); /* enable test-tone mode */
AD9911_reg[CTW0] = _AD9911_FTW_from_freq( freq );
_AD9911_IOupdate();
_AD9911_select_channel( 0 ); /* all registers are written to channel 0 on IOUpdate */
AD9911_reg[CFR] &= ~((uint32_t)1<<7); /* disable digital power down */
AD9911_reg[CFR] |= ((uint32_t)7<<16); /* set aux channel amp to -6dB */
AD9911_reg[CTW0] = _AD9911_FTW_from_freq( freq2 );
AD9911_reg[ACR] = (uint32_t)amp & AD9911_ampmask;
_AD9911_IOupdate();
}
/* untested */
void AD9911_linear_sweep_freq( uint32_t S0, uint32_t E0 )
{
_AD9911_select_channel( 1 ); /* all registers are written to channel 1 on IOUpdate */
AD9911_reg[FR1] &= ~((uint32_t)1<<5); /* enable SYNC_CLK pin */
AD9911_reg[CTW0] = (uint32_t)S0;
AD9911_reg[CTW1] = (uint32_t)E0;
AD9911_reg[CFR] &= ~((uint32_t)3<<22);
AD9911_reg[CFR] |= ((uint32_t)2<<22); /* enable frequency sweep */
AD9911_reg[FR1] &= ~((uint32_t)3<<8);
AD9911_reg[FR1] |= ((uint32_t)0<<8); /* has to be 2-level modulation */
AD9911_reg[CFR] |= ((uint32_t)1<<14)|((uint32_t)1<<15); /* enable linear sweep, set no dwell mode (reset to 0 on reaching E0) */
_AD9911_IOupdate();
}