-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAD9832.cpp
executable file
·151 lines (130 loc) · 4.73 KB
/
AD9832.cpp
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
/***************************************************************************
* *
* 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 2 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, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: AD9832.cpp *
* *
***************************************************************************/
#include "armlib.h"
/// Number of digits in DDS frequency to FTW conversion.
const uint32_t DDSFreqToFTWDigits = 9;
/// Array of multiplication factors used to convert frequency to the FTW.
const uint32_t DDSMult[2] [DDSFreqToFTWDigits] =
{
{ 174, 9, 2, 2, 8, 1, 3, 2, 9 },
{ 171, 7, 9, 8, 6, 9, 1, 8, 4 }
};
/// Array of divisors used to convert frequency to the FTW.
const uint32_t DDSDivisor[DDSFreqToFTWDigits - 1] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
/// Reserve memory for singleton object.
static AD9832 ad9832SingletonObject;
/**
* Constructor.
*/
AD9832::AD9832()
{
this->referenceFrequency = Freq25000000Hz;
}
/**
* Get a pointer to the SPI0 object.
*/
AD9832 *AD9832::GetInstance()
{
return &ad9832SingletonObject;
}
/**
* Initialize the DDS regsiters and RAM.
*/
void AD9832::Enable()
{
// Set default I/O for the DDS.
IOSetCS (true);
IOSetProfileSelect (Profile0);
SetMode (Sleep);
// Set SYNC to 1 and SELSRC to 0.
WriteRegister (0xa000);
}
/**
* Write a 16-bit control register in the AD9832.
*
* @param data 16-bit data to write
*/
void AD9832::WriteRegister (uint32_t data)
{
IOSetCS (false);
SPIWrite ((data >>8) & 0x00ff);
SPIWrite (data & 0xff);
IOSetCS (true);
}
/**
* Set the DDS frequency.
*
* @param freq frequency in Hertz
*
*/
void AD9832::SetFreq(uint32_t freq)
{
uint32_t i, ftw;
// To avoid rounding errors with floating point math, we do a long multiply on the data.
ftw = freq * DDSMult[this->referenceFrequency][0];
for (i = 0; i < DDSFreqToFTWDigits - 1; ++i)
ftw += (freq * DDSMult[this->referenceFrequency][i+1]) / DDSDivisor[i];
SetFTW (ftw);
}
/**
* Set the DDS operational mode.
*
* @param mode specifies the desired mode
*/
void AD9832::SetMode(DDSMode mode)
{
switch (mode)
{
case Sleep:
WriteRegister (0xf800);
break;
case PowerOn:
WriteRegister (0xc000);
break;
} // END switch
}
/**
* Set DDS frequency tuning word. The output frequency is equal to MClk * (ftw / 2 ^ 32).
*
* @param ftw Frequency Tuning Word
*/
void AD9832::SetFTW (uint32_t ftw)
{
// Write FREQ0 8-bit defer and 16-bit registers.
WriteRegister (0x3300 | ((ftw >> 24) & 0xff));
WriteRegister (0x2200 | ((ftw >> 16) & 0xff));
WriteRegister (0x3100 | ((ftw >> 8) & 0xff));
WriteRegister (0x2000 | (ftw & 0xff));
}
/**
* Set the reference clock frequency.
*
* @param referenceFrequency reference clock value
*/
void AD9832::SetReferenceFrequency (ReferenceFrequency referenceFrequency)
{
this->referenceFrequency = referenceFrequency;
}