Communication.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /***************************************************************************//**
  2. * @file Communication.c
  3. * @brief Implementation of Communication Driver for RENESAS RX62N
  4. * Processor.
  5. * @author DBogdan (dragos.bogdan@analog.com)
  6. ********************************************************************************
  7. * Copyright 2012(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. * SVN Revision: 381
  42. *******************************************************************************/
  43. /******************************************************************************/
  44. /* Include Files */
  45. /******************************************************************************/
  46. #include "Communication.h"
  47. /***************************************************************************//**
  48. * @brief Initializes the SPI communication peripheral.
  49. *
  50. * @param lsbFirst - Transfer format (0 or 1).
  51. * Example: 0x0 - MSB first.
  52. * 0x1 - LSB first.
  53. * @param clockFreq - SPI clock frequency (Hz).
  54. * Example: 1000 - SPI clock frequency is 1 kHz.
  55. * @param clockPol - SPI clock polarity (0 or 1).
  56. * Example: 0x0 - idle state for SPI clock is low.
  57. * 0x1 - idle state for SPI clock is high.
  58. * @param clockPha - SPI clock phase (0 or 1).
  59. * Example: 0x0 - data is latched on the leading edge of SPI
  60. * clock and data changes on trailing edge.
  61. * 0x1 - data is latched on the trailing edge of SPI
  62. * clock and data changes on the leading edge.
  63. *
  64. * @return 0 - Initialization failed, 1 - Initialization succeeded.
  65. *******************************************************************************/
  66. unsigned char SPI_Init(unsigned char lsbFirst,
  67. unsigned long clockFreq,
  68. unsigned char clockPol,
  69. unsigned char clockPha)
  70. {
  71. MISOA_PIN_IN;
  72. MOSIA_PIN_OUT;
  73. ST7579_CS_PIN_OUT;
  74. ST7579_CS_HIGH;
  75. AD7793_CS_PIN_OUT;
  76. AD7793_CS_HIGH;
  77. R_SPI_Create(0, // Channel selection.
  78. PDL_SPI_MODE_SPI_MASTER | // Connection mode.
  79. PDL_SPI_PIN_A | // A pins for signal MISO, MOSI, RSPCK, SSL0, SSL1, SSL2 and SSL3.
  80. PDL_SPI_PIN_MOSI_IDLE_LOW, // The MOSI output state when no SSLn pin is active.
  81. PDL_SPI_FRAME_1_1, // Frame configuration selection.
  82. PDL_NO_DATA, // Extended timing control - default settings.
  83. 0x80000000); // Bit rate or register value.
  84. R_SPI_Command(0, // Channel selection.
  85. 0, // Command selection.
  86. PDL_SPI_CLOCK_MODE_3 | // Clock is low when idle; data is sampled on the rising edge.
  87. PDL_SPI_DIV_8 | // Bit rate (specified for R_SPI_Create) : 8.
  88. PDL_SPI_LENGTH_8 | // The number of bits in the frame transfer.
  89. PDL_SPI_MSB_FIRST, // Most-significant bit first.
  90. PDL_NO_DATA); // Extended timing control - default settings.
  91. return(1);
  92. }
  93. /***************************************************************************//**
  94. * @brief Writes data to SPI.
  95. *
  96. * @param data - Write data buffer:
  97. * - first byte is the chip select number;
  98. * - from the second byte onwards are located data bytes to write.
  99. * @param bytesNumber - Number of bytes to write.
  100. *
  101. * @return Number of written bytes.
  102. *******************************************************************************/
  103. unsigned char SPI_Write(unsigned char* data,
  104. unsigned char bytesNumber)
  105. {
  106. unsigned char chipSelect = data[0];
  107. unsigned char writeData[4] = {0, 0, 0, 0};
  108. unsigned char byte = 0;
  109. for(byte = 0;byte < bytesNumber;byte ++)
  110. {
  111. writeData[byte] = data[byte + 1];
  112. }
  113. if(chipSelect == 1)
  114. {
  115. AD7793_CS_LOW;
  116. }
  117. if(chipSelect == 2)
  118. {
  119. ST7579_CS_LOW;
  120. }
  121. for(byte = 0;byte < bytesNumber;byte ++)
  122. {
  123. R_SPI_Transfer(0, // Channel selection.
  124. PDL_NO_DATA, // DMAC / DTC control.
  125. (unsigned long*)&writeData[byte], // Transmit data start address.
  126. PDL_NO_PTR, // Receive data start address.
  127. 1, // Sequence loop count.
  128. PDL_NO_FUNC, // Callback function.
  129. 0); // Interrupt priority level.
  130. }
  131. if(chipSelect == 1)
  132. {
  133. AD7793_CS_HIGH;
  134. }
  135. if(chipSelect == 2)
  136. {
  137. ST7579_CS_HIGH;
  138. }
  139. return(bytesNumber);
  140. }
  141. /***************************************************************************//**
  142. * @brief Reads data from SPI.
  143. *
  144. * @param data - As an input parameter, data represents the write buffer:
  145. * - first byte is the chip select number;
  146. * - from the second byte onwards are located data bytes to write.
  147. * As an output parameter, data represents the read buffer:
  148. * - from the first byte onwards are located the read data bytes.
  149. * @param bytesNumber - Number of bytes to write.
  150. *
  151. * @return Number of written bytes.
  152. *******************************************************************************/
  153. unsigned char SPI_Read(unsigned char* data,
  154. unsigned char bytesNumber)
  155. {
  156. unsigned char chipSelect = data[0];
  157. unsigned char writeData[4] = {0, 0, 0, 0};
  158. unsigned char byte = 0;
  159. for(byte = 0;byte < bytesNumber;byte ++)
  160. {
  161. writeData[byte] = data[byte + 1];
  162. }
  163. if(chipSelect == 1)
  164. {
  165. AD7793_CS_LOW;
  166. }
  167. if(chipSelect == 2)
  168. {
  169. ST7579_CS_LOW;
  170. }
  171. for(byte = 0;byte < bytesNumber;byte ++)
  172. {
  173. R_SPI_Transfer(0, // Channel selection.
  174. PDL_NO_DATA, // DMAC / DTC control.
  175. (unsigned long*)&writeData[byte], // Transmit data start address.
  176. (unsigned long*)&data[byte], // Transmit data start address.
  177. 1, // Sequence loop count.
  178. PDL_NO_FUNC, // Callback function.
  179. 0); // Interrupt priority level.
  180. }
  181. if(chipSelect == 1)
  182. {
  183. AD7793_CS_HIGH;
  184. }
  185. if(chipSelect == 2)
  186. {
  187. ST7579_CS_HIGH;
  188. }
  189. return(bytesNumber);
  190. }
  191. /***************************************************************************//**
  192. * @brief Initializes the I2C communication peripheral.
  193. *
  194. * @param clockFreq - I2C clock frequency (Hz).
  195. * Example: 100000 - I2C clock frequency is 100 kHz.
  196. *
  197. * @return 0 - Initialization failed, 1 - Initialization succeeded.
  198. *******************************************************************************/
  199. unsigned char I2C_Init(unsigned long clockFreq)
  200. {
  201. R_IIC_Create(0, // Channel selection.
  202. PDL_IIC_MODE_IIC | // I2C Bus.
  203. PDL_IIC_INT_PCLK_DIV_8, // The reference clock source,
  204. // used inside the I2C module.
  205. PDL_NO_DATA, // Detection configuration.
  206. PDL_NO_DATA, // Slave address.
  207. PDL_NO_DATA, // Slave address.
  208. PDL_NO_DATA, // Slave address.
  209. 100E3, // Transfer rate control.
  210. 0); // Rise and fall time correction.
  211. return(1);
  212. }
  213. /***************************************************************************//**
  214. * @brief Writes data to I2C.
  215. *
  216. * @param data - Write data buffer:
  217. * - first byte is the slave address;
  218. * - from the second byte onwards are located data bytes.
  219. * @param bytesNumber - Number of bytes to write.
  220. *
  221. * @return Number of written bytes.
  222. *******************************************************************************/
  223. unsigned char I2C_Write(unsigned char* data,
  224. unsigned char bytesNumber)
  225. {
  226. unsigned char slaveAddress = data[0];
  227. unsigned char writeData[4] = {0, 0, 0, 0};
  228. unsigned char byte = 0;
  229. for(byte = 0;byte < bytesNumber;byte ++)
  230. {
  231. writeData[byte] = data[byte + 1];
  232. }
  233. R_IIC_MasterSend(0,
  234. PDL_IIC_START_ENABLE | PDL_IIC_STOP_ENABLE,
  235. (slaveAddress << 1 | 0x00),
  236. (uint8_t*)writeData,
  237. bytesNumber,
  238. PDL_NO_FUNC,
  239. 0);
  240. return(bytesNumber);
  241. }
  242. /***************************************************************************//**
  243. * @brief Reads data from I2C.
  244. *
  245. * @param data - Read data buffer:
  246. * As an input parameter data must have 2 bytes:
  247. * - first byte is the slave address;
  248. * - second byte is the register address; if this byte is 0xFF the write
  249. * operation is not executed.
  250. * As an output parameter from the first byte onwards are located
  251. * data bytes.
  252. * @param bytesNumber - number of bytes to read.
  253. *
  254. * @return Number of read bytes.
  255. *******************************************************************************/
  256. unsigned char I2C_Read(unsigned char* data, unsigned char bytesNumber)
  257. {
  258. unsigned char slaveAddress = data[0];
  259. unsigned char registerAddress = data[1];
  260. slaveAddress = (slaveAddress << 1);
  261. if(registerAddress != 0xFF)
  262. {
  263. R_IIC_MasterSend(0,
  264. PDL_IIC_START_ENABLE | PDL_IIC_STOP_DISABLE,
  265. slaveAddress,
  266. (uint8_t*)&registerAddress,
  267. 1,
  268. PDL_NO_FUNC,
  269. 0);
  270. }
  271. slaveAddress += 1;
  272. R_IIC_MasterReceive(0,
  273. PDL_NO_DATA,
  274. (slaveAddress << 1 | 0x01),
  275. (uint8_t*)data,
  276. bytesNumber,
  277. PDL_NO_FUNC,
  278. 0);
  279. return(bytesNumber);
  280. }