Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/ADF7023/ADF7023.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADF7023.c
3 * @brief Implementation of ADF7023 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 ********************************************************************************
40 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "ADF7023.h"
47 #include "ADF7023_Config.h"
48 #include "Communication.h"
49
50 /******************************************************************************/
51 /*************************** Macros Definitions *******************************/
52 /******************************************************************************/
53 #define ADF7023_CS_ASSERT CS_PIN_LOW
54 #define ADF7023_CS_DEASSERT CS_PIN_HIGH
55 #define ADF7023_MISO MISO_PIN
56
57 /******************************************************************************/
58 /************************ Variables Definitions *******************************/
59 /******************************************************************************/
60 struct ADF7023_BBRAM ADF7023_BBRAMCurrent;
61
62 /***************************************************************************//**
63 * @brief Transfers one byte of data.
64 *
65 * @param writeByte - Write data.
66 * @param readByte - Read data.
67 *
68 * @return None.
69 *******************************************************************************/
70 void ADF7023_WriteReadByte(unsigned char writeByte,
71 unsigned char* readByte)
72 {
73 unsigned char data = 0;
74
75 data = writeByte;
76 SPI_Read(0, &data, 1);
77 if(readByte)
78 {
79 *readByte = data;
80 }
81 }
82
83 /***************************************************************************//**
84 * @brief Initializes the ADF7023.
85 *
86 * @return retVal - Result of the initialization procedure.
87 * Example: 0 - if initialization was successful;
88 * -1 - if initialization was unsuccessful.
89 *******************************************************************************/
90 char ADF7023_Init(void)
91 {
92 char retVal = 0;
93 unsigned char miso = 0;
94 unsigned short timeout = 0;
95 unsigned char status = 0;
96
97 ADF7023_BBRAMCurrent = ADF7023_BBRAMDefault;
98 SPI_Init(0, // MSB first.
99 1000000, // Clock frequency.
100 0, // Idle state for clock is a high level; active state is a low level.
101 1); // Serial output data changes on transition from idle clock state to active clock state.
102 ADF7023_CS_ASSERT;
103 while ((miso == 0) && (timeout < 1000))
104 {
105 miso = ADF7023_MISO;
106 timeout++;
107 }
108 if(timeout == 1000)
109 {
110 retVal = -1;
111 }
112 while(!(status & STATUS_CMD_READY))
113 {
114 ADF7023_GetStatus(&status);
115 }
116 ADF7023_SetRAM(0x100, 64, (unsigned char*)&ADF7023_BBRAMCurrent);
117 ADF7023_SetCommand(CMD_CONFIG_DEV);
118
119 return retVal;
120 }
121
122 /***************************************************************************//**
123 * @brief Reads the status word of the ADF7023.
124 *
125 * @param status - Status word.
126 *
127 * @return None.
128 *******************************************************************************/
129 void ADF7023_GetStatus(unsigned char* status)
130 {
131 ADF7023_CS_ASSERT;
132 ADF7023_WriteReadByte(SPI_NOP, 0);
133 ADF7023_WriteReadByte(SPI_NOP, status);
134 ADF7023_CS_DEASSERT;
135 }
136
137 /***************************************************************************//**
138 * @brief Initiates a command.
139 *
140 * @param command - Command.
141 *
142 * @return None.
143 *******************************************************************************/
144 void ADF7023_SetCommand(unsigned char command)
145 {
146 ADF7023_CS_ASSERT;
147 ADF7023_WriteReadByte(command, 0);
148 ADF7023_CS_DEASSERT;
149 }
150
151 /***************************************************************************//**
152 * @brief Sets a FW state and waits until the device enters in that state.
153 *
154 * @param fwState - FW state.
155 *
156 * @return None.
157 *******************************************************************************/
158 void ADF7023_SetFwState(unsigned char fwState)
159 {
160 unsigned char status = 0;
161
162 switch(fwState)
163 {
164 case FW_STATE_PHY_OFF:
165 ADF7023_SetCommand(CMD_PHY_OFF);
166 break;
167 case FW_STATE_PHY_ON:
168 ADF7023_SetCommand(CMD_PHY_ON);
169 break;
170 case FW_STATE_PHY_RX:
171 ADF7023_SetCommand(CMD_PHY_RX);
172 break;
173 case FW_STATE_PHY_TX:
174 ADF7023_SetCommand(CMD_PHY_TX);
175 break;
176 default:
177 ADF7023_SetCommand(CMD_PHY_SLEEP);
178 }
179 while((status & STATUS_FW_STATE) != fwState)
180 {
181 ADF7023_GetStatus(&status);
182 }
183 }
184
185 /***************************************************************************//**
186 * @brief Reads data from the RAM.
187 *
188 * @param address - Start address.
189 * @param length - Number of bytes to write.
190 * @param data - Read buffer.
191 *
192 * @return None.
193 *******************************************************************************/
194 void ADF7023_GetRAM(unsigned long address,
195 unsigned long length,
196 unsigned char* data)
197 {
198 ADF7023_CS_ASSERT;
199 ADF7023_WriteReadByte(SPI_MEM_RD | ((address & 0x700) >> 8), 0);
200 ADF7023_WriteReadByte(address & 0xFF, 0);
201 ADF7023_WriteReadByte(SPI_NOP, 0);
202 while(length--)
203 {
204 ADF7023_WriteReadByte(SPI_NOP, data++);
205 }
206 ADF7023_CS_DEASSERT;
207 }
208
209 /***************************************************************************//**
210 * @brief Writes data to RAM.
211 *
212 * @param address - Start address.
213 * @param length - Number of bytes to write.
214 * @param data - Write buffer.
215 *
216 * @return None.
217 *******************************************************************************/
218 void ADF7023_SetRAM(unsigned long address,
219 unsigned long length,
220 unsigned char* data)
221 {
222 ADF7023_CS_ASSERT;
223 ADF7023_WriteReadByte(SPI_MEM_WR | ((address & 0x700) >> 8), 0);
224 ADF7023_WriteReadByte(address & 0xFF, 0);
225 while(length--)
226 {
227 ADF7023_WriteReadByte(*(data++), 0);
228 }
229 ADF7023_CS_DEASSERT;
230 }
231
232 /***************************************************************************//**
233 * @brief Receives one packet.
234 *
235 * @param packet - Data buffer.
236 * @param length - Number of received bytes.
237 *
238 * @return None.
239 *******************************************************************************/
240 void ADF7023_ReceivePacket(unsigned char* packet, unsigned char* length)
241 {
242 unsigned char interruptReg = 0;
243
244 ADF7023_SetFwState(FW_STATE_PHY_ON);
245 ADF7023_SetFwState(FW_STATE_PHY_RX);
246 while(!(interruptReg & BBRAM_INTERRUPT_MASK_0_INTERRUPT_CRC_CORRECT))
247 {
248 ADF7023_GetRAM(MCR_REG_INTERRUPT_SOURCE_0,
249 0x1,
250 &interruptReg);
251 }
252 ADF7023_SetRAM(MCR_REG_INTERRUPT_SOURCE_0,
253 0x1,
254 &interruptReg);
255 ADF7023_GetRAM(0x10, 1, length);
256 ADF7023_GetRAM(0x12, *length - 2, packet);
257 }
258
259 /***************************************************************************//**
260 * @brief Transmits one packet.
261 *
262 * @param packet - Data buffer.
263 * @param length - Number of bytes to transmit.
264 *
265 * @return None.
266 *******************************************************************************/
267 void ADF7023_TransmitPacket(unsigned char* packet, unsigned char length)
268 {
269 unsigned char interruptReg = 0;
270 unsigned char header[2] = {0, 0};
271
272 header[0] = 2 + length;
273 header[1] = ADF7023_BBRAMCurrent.addressMatchOffset;
274 ADF7023_SetRAM(0x10, 2, header);
275 ADF7023_SetRAM(0x12, length, packet);
276 ADF7023_SetFwState(FW_STATE_PHY_ON);
277 ADF7023_SetFwState(FW_STATE_PHY_TX);
278 while(!(interruptReg & BBRAM_INTERRUPT_MASK_0_INTERRUPT_TX_EOF))
279 {
280 ADF7023_GetRAM(MCR_REG_INTERRUPT_SOURCE_0,
281 0x1,
282 &interruptReg);
283 }
284 }
285
286 /***************************************************************************//**
287 * @brief Sets the channel frequency.
288 *
289 * @param chFreq - Channel frequency.
290 *
291 * @return None.
292 *******************************************************************************/
293 void ADF7023_SetChannelFrequency(unsigned long chFreq)
294 {
295 chFreq = (unsigned long)(((float)chFreq / 26000000) * 65535);
296 ADF7023_BBRAMCurrent.channelFreq0 = (chFreq & 0x0000FF) >> 0;
297 ADF7023_BBRAMCurrent.channelFreq1 = (chFreq & 0x00FF00) >> 8;
298 ADF7023_BBRAMCurrent.channelFreq2 = (chFreq & 0xFF0000) >> 16;
299 ADF7023_SetRAM(0x100, 64, (unsigned char*)&ADF7023_BBRAMCurrent);
300 }
301
302 /***************************************************************************//**
303 * @brief Sets the data rate.
304 *
305 * @param dataRate - Data rate.
306 *
307 * @return None.
308 *******************************************************************************/
309 void ADF7023_SetDataRate(unsigned long dataRate)
310 {
311 unsigned char status = 0;
312
313 dataRate = (unsigned long)(dataRate / 100);
314 ADF7023_BBRAMCurrent.radioCfg0 =
315 BBRAM_RADIO_CFG_0_DATA_RATE_7_0((dataRate & 0x00FF) >> 0);
316 ADF7023_BBRAMCurrent.radioCfg1 &= ~BBRAM_RADIO_CFG_1_DATA_RATE_11_8(0xF);
317 ADF7023_BBRAMCurrent.radioCfg1 |=
318 BBRAM_RADIO_CFG_1_DATA_RATE_11_8((dataRate & 0x0F00) >> 8);
319 ADF7023_SetRAM(0x100, 64, (unsigned char*)&ADF7023_BBRAMCurrent);
320 ADF7023_SetFwState(FW_STATE_PHY_OFF);
321 ADF7023_SetCommand(CMD_CONFIG_DEV);
322 }
323
324 /***************************************************************************//**
325 * @brief Sets the frequency deviation.
326 *
327 * @param freqDev - Frequency deviation.
328 *
329 * @return None.
330 *******************************************************************************/
331 void ADF7023_SetFrequencyDeviation(unsigned long freqDev)
332 {
333 unsigned char status = 0;
334
335 freqDev = (unsigned long)(freqDev / 100);
336 ADF7023_BBRAMCurrent.radioCfg1 &=
337 ~BBRAM_RADIO_CFG_1_FREQ_DEVIATION_11_8(0xF);
338 ADF7023_BBRAMCurrent.radioCfg1 |=
339 BBRAM_RADIO_CFG_1_FREQ_DEVIATION_11_8((freqDev & 0x0F00) >> 8);
340 ADF7023_BBRAMCurrent.radioCfg2 =
341 BBRAM_RADIO_CFG_2_FREQ_DEVIATION_7_0((freqDev & 0x00FF) >> 0);
342 ADF7023_SetRAM(0x100, 64, (unsigned char*)&ADF7023_BBRAMCurrent);
343 ADF7023_SetFwState(FW_STATE_PHY_OFF);
344 ADF7023_SetCommand(CMD_CONFIG_DEV);
345 }