Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Microchip/PIC32MX320F128H/Common/Communication.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Communication.c
3 * @brief Implementation of Communication Driver for PIC32MX320F128H
4 Processor.
5 * @author DBogdan ([email protected])
6 ********************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************/
41
42 /******************************************************************************/
43 /***************************** Include Files **********************************/
44 /******************************************************************************/
45 #include "Communication.h" /*!< Communication definitions */
46
47 /******************************************************************************/
48 /************************ Functions Definitions *******************************/
49 /******************************************************************************/
50
51 /***************************************************************************//**
52 * @brief Initializes the SPI communication peripheral.
53 *
54 * @param lsbFirst - Transfer format (0 or 1).
55 * Example: 0x0 - MSB first.
56 * 0x1 - LSB first.
57 * @param clockFreq - SPI clock frequency (Hz).
58 * Example: 1000 - SPI clock frequency is 1 kHz.
59 * @param clockPol - SPI clock polarity (0 or 1).
60 * Example: 0x0 - Idle state for clock is a low level; active
61 * state is a high level;
62 * 0x1 - Idle state for clock is a high level; active
63 * state is a low level.
64 * @param clockEdg - SPI clock edge (0 or 1).
65 * Example: 0x0 - Serial output data changes on transition
66 * from idle clock state to active clock state;
67 * 0x1 - Serial output data changes on transition
68 * from active clock state to idle clock state.
69 *
70 * @return status - Result of the initialization procedure.
71 * Example: 0 - if initialization was successful;
72 * -1 - if initialization was unsuccessful.
73 *******************************************************************************/
74 unsigned char SPI_Init(unsigned char lsbFirst,
75 unsigned long clockFreq,
76 unsigned char clockPol,
77 unsigned char clockEdg)
78 {
79 unsigned char status = 0;
80 unsigned long pbFrequency = 80000000;
81 unsigned short brgValue = 0;
82
83 SPI_CS_HIGH;
84 SPI_CS_PIN_OUT;
85 /*!< Fsck = Fpb / (2 * (SPIxBRG + 1)) */
86 brgValue = pbFrequency / (2 * clockFreq) - 1;
87 SPI2CON = 0; /*!< Clear the content of SPI2CON register */
88 SPI2BRG = brgValue;
89 SPI2CONbits.CKE = clockEdg; /*!< SPI Clock Edge Select bit */
90 SPI2CONbits.CKP = clockPol; /*!< Clock Polarity Select bit */
91 SPI2CONbits.MSTEN = 1; /*!< Enable master mode */
92 SPI2CONbits.ON = 1; /*!< Enable SPI peripheral */
93
94 return status;
95 }
96
97 /***************************************************************************//**
98 * @brief Writes data to SPI.
99 *
100 * @param slaveDeviceId - The ID of the selected slave device.
101 * @param data - Data represents the write buffer.
102 * @param bytesNumber - Number of bytes to write.
103 *
104 * @return Number of written bytes.
105 *******************************************************************************/
106 unsigned char SPI_Write(unsigned char slaveDeviceId,
107 unsigned char* data,
108 unsigned char bytesNumber)
109 {
110 unsigned char byte = 0;
111 unsigned char tempByte = 0;
112
113 if(slaveDeviceId == 1)
114 {
115 SPI_CS_LOW;
116 }
117 for(byte = 0; byte < bytesNumber; byte++)
118 {
119 SPI2BUF = data[byte];
120 while(SPI2STATbits.SPIRBF == 0);
121 tempByte = SPI2BUF;
122 }
123 while(SPI2STATbits.SPIBUSY == 1);
124 if(slaveDeviceId == 1)
125 {
126 SPI_CS_HIGH;
127 }
128
129 return bytesNumber;
130 }
131
132 /***************************************************************************//**
133 * @brief Reads data from SPI.
134 *
135 * @param slaveDeviceId - The ID of the selected slave device.
136 * @param data - Data represents the read buffer.
137 * @param bytesNumber - Number of bytes to read.
138 *
139 * @return Number of read bytes.
140 *******************************************************************************/
141 unsigned char SPI_Read(unsigned char slaveDeviceId,
142 unsigned char* data,
143 unsigned char bytesNumber)
144 {
145 unsigned char byte = 0;
146 unsigned char writeBuffer[4] = {0, 0, 0, 0};
147
148 for(byte = 0; byte < bytesNumber; byte++)
149 {
150 writeBuffer[byte] = data[byte];
151 }
152 if(slaveDeviceId == 1)
153 {
154 SPI_CS_LOW;
155 }
156 for(byte = 0; byte < bytesNumber; byte++)
157 {
158 SPI2BUF = writeBuffer[byte];
159 while(SPI2STATbits.SPIRBF == 0);
160 data[byte] = SPI2BUF;
161 }
162 while(SPI2STATbits.SPIBUSY == 1);
163 if(slaveDeviceId == 1)
164 {
165 SPI_CS_HIGH;
166 }
167
168 return bytesNumber;
169 }
170
171 /***************************************************************************//**
172 * @brief Initializes the I2C communication peripheral.
173 *
174 * @param clockFreq - I2C clock frequency (Hz).
175 * Example: 100000 - I2C clock frequency is 100 kHz.
176 * @return status - Result of the initialization procedure.
177 * Example: 0 - if initialization was successful;
178 * -1 - if initialization was unsuccessful.
179 *******************************************************************************/
180 unsigned char I2C_Init(unsigned long clockFreq)
181 {
182 unsigned char status = 0;
183 unsigned long pbFrequency = 80000000;
184 unsigned short brgValue = 0;
185
186 I2C1CON = 0; /*!< Clear the content of I2C1CON register */
187 /*!< Fsck = Fpb / ((I2CxBRG + 2) * 2) */
188 brgValue = pbFrequency / (2 * clockFreq) - 2;
189 I2C1BRG = brgValue;
190 I2C1CONbits.ON = 1; /*!< Enable the I2C module and configure the SDA and */
191 /*!< SCL pins as serial port pins */
192 I2C1CONbits.ACKEN = 1;
193 return status;
194 }
195
196 /***************************************************************************//**
197 * @brief Writes data to a slave device.
198 *
199 * @param slaveAddress - Adress of the slave device.
200 * @param dataBuffer - Pointer to a buffer storing the transmission data.
201 * @param bytesNumber - Number of bytes to write.
202 * @param stopBit - Stop condition control.
203 * Example: 0 - A stop condition will not be sent;
204 * 1 - A stop condition will be sent.
205 *
206 * @return status - Number of read bytes or 0xFF if the slave address was not
207 * acknowledged by the device.
208 *******************************************************************************/
209 unsigned char I2C_Write(unsigned char slaveAddress,
210 unsigned char* dataBuffer,
211 unsigned char bytesNumber,
212 unsigned char stopBit)
213 {
214 unsigned char status = 0;
215 unsigned char acknowledge = 0;
216 unsigned char byte = 0;
217
218 I2C1CONbits.SEN = 1; /*!< Initiate a start condition */
219 while(I2C1CONbits.SEN); /*!< Wait for start condition to complete */
220 I2C1TRN = slaveAddress << 1;
221 while(I2C1STATbits.TRSTAT); /*!< Wait for transmition to complete */
222 acknowledge = I2C1STATbits.ACKSTAT;
223 if(acknowledge == 0) /*!< Acknowledge was received */
224 {
225 for(byte = 0; byte < bytesNumber; byte++)
226 {
227 I2C1TRN = dataBuffer[byte];
228 while(I2C1STATbits.TRSTAT); /*!< Wait for transmition to complete */
229 }
230 }
231 else
232 {
233 status = 0xFF;
234 }
235 if(stopBit)
236 {
237 I2C1CONbits.PEN = 1; /*!< Initiate a stop condition */
238 while(I2C1CONbits.PEN); /*!< Wait for stop condition to copmlete */
239 }
240
241 return status;
242 }
243
244 /***************************************************************************//**
245 * @brief Reads data from a slave device.
246 *
247 * @param slaveAddress - Adress of the slave device.
248 * @param dataBuffer - Pointer to a buffer that will store the received data.
249 * @param bytesNumber - Number of bytes to read.
250 * @param stopBit - Stop condition control.
251 * Example: 0 - A stop condition will not be sent;
252 * 1 - A stop condition will be sent.
253 *
254 * @return status - Number of read bytes or 0xFF if the slave address was not
255 * acknowledged by the device.
256 *******************************************************************************/
257 unsigned char I2C_Read(unsigned char slaveAddress,
258 unsigned char* dataBuffer,
259 unsigned char bytesNumber,
260 unsigned char stopBit)
261 {
262 unsigned char status = bytesNumber;
263 unsigned char acknowledge = 0;
264 unsigned char byte = 0;
265
266 I2C1CONbits.SEN = 1; /*!< Initiate a start condition */
267 while(I2C1CONbits.SEN); /*!< Wait for start condition to complete */
268 I2C1TRN = (slaveAddress << 1) + 1;
269 while(I2C1STATbits.TRSTAT); /*!< Wait for transmition to complete */
270 acknowledge = I2C1STATbits.ACKSTAT;
271 if(acknowledge == 0) /*!< Acknowledge was received */
272 {
273 for(byte = 0; byte < bytesNumber; byte++)
274 {
275 I2C1CONbits.RCEN = 1; /*!< Enable receive mode for I2C */
276 if(byte == (bytesNumber - 1))
277 {
278 I2C1CONbits.ACKDT = 1;
279 }
280 else
281 {
282 I2C1CONbits.ACKDT = 0;
283 }
284 while(I2C1CONbits.RCEN); /*!< Wait for reception to complete */
285 I2C1CONbits.ACKEN = 1;
286 while(I2C1CONbits.ACKEN);
287 dataBuffer[byte] = I2C1RCV;
288 }
289 }
290 else
291 {
292 status = 0xFF;
293 }
294 I2C1CONbits.PEN = 1; /*!< Initiate a stop condition */
295 while(I2C1CONbits.PEN); /*!< Wait for stop condition to copmlete */
296
297 return status;
298 }
299
300 /***************************************************************************//**
301 * @brief Initializes the UART communication peripheral.
302 *
303 * @param baudRate - Baud rate value.
304 * Example: 9600 - 9600 bps.
305 *
306 * @return status - Result of the initialization procedure.
307 * Example: -1 - if initialization was unsuccessful;
308 * 0 - if initialization was successful.
309 *******************************************************************************/
310 char UART_Init(unsigned long baudRate)
311 {
312 unsigned long pbFrequency = 80000000;
313 unsigned short brgValue = 0;
314
315 /*!< BaudRate = Fpb / (16 * (UxBRG + 1)) */
316 brgValue = pbFrequency / (16 * baudRate) - 1;
317 U1MODE = 0; /*!< Clear the content of U1MODE register */
318 U1BRG = brgValue;
319 U1MODEbits.PDSEL1 = 0;
320 U1MODEbits.PDSEL0 = 0; /*!< 8-bit data, no parity */
321 U1MODEbits.STSEL = 0; /*!< 1 Stop bit */
322 U1STAbits.URXEN = 1; /*!< Enable UART1 receiver */
323 U1STAbits.UTXEN = 1; /*!< Enable UART1 transmitter */
324 U1MODEbits.ON = 1; /*!< Enable UART1 peripheral */
325
326 return 0;
327 }
328
329 /***************************************************************************//**
330 * @brief Writes one character to UART.
331 *
332 * @param data - Character to write.
333 *
334 * @return None.
335 *******************************************************************************/
336 void UART_WriteChar(char data)
337 {
338 while(U1STAbits.TRMT == 0);
339 U1TXREG = data;
340 }
341
342 /***************************************************************************//**
343 * @brief Reads one character from UART.
344 *
345 * @param data - Read character.
346 *
347 * @return None.
348 *******************************************************************************/
349 void UART_ReadChar(char* data)
350 {
351 while(U1STAbits.URXDA == 0);
352 *data = U1RXREG;
353 }
354
355 /***************************************************************************//**
356 * @brief Writes one string of characters to UART.
357 *
358 * @param string - String of characters to write.
359 *
360 * @return None.
361 *******************************************************************************/
362 void UART_WriteString(const char* string)
363 {
364 while(*string)
365 {
366 UART_WriteChar(*string++);
367 }
368 }