Communication.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /***************************************************************************//**
  2. * @file Communication.c
  3. * @brief Implementation of Communication Driver for RENESAS RL78G13
  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: 499
  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. ST7579_CS_PIN_OUT;
  72. ST7579_CS_HIGH;
  73. ADI_PART_CS_PIN_OUT;
  74. ADI_PART_CS_HIGH;
  75. R_CSI10_Start();
  76. return(1);
  77. }
  78. /***************************************************************************//**
  79. * @brief Writes data to SPI.
  80. *
  81. * @param data - Write data buffer:
  82. * - first byte is the chip select number;
  83. * - from the second byte onwards are located data bytes to write.
  84. * @param bytesNumber - Number of bytes to write.
  85. *
  86. * @return Number of written bytes.
  87. *******************************************************************************/
  88. unsigned char SPI_Write(unsigned char* data,
  89. unsigned char bytesNumber)
  90. {
  91. unsigned char chipSelect = data[0];
  92. unsigned char writeData[4] = {0, 0, 0, 0};
  93. unsigned char readData[4] = {0, 0, 0, 0};
  94. unsigned char byte = 0;
  95. for(byte = 0;byte < bytesNumber;byte ++)
  96. {
  97. writeData[byte] = data[byte + 1];
  98. }
  99. if(chipSelect == 1)
  100. {
  101. ADI_PART_CS_LOW;
  102. }
  103. if(chipSelect == 2)
  104. {
  105. ST7579_CS_LOW;
  106. }
  107. for(byte = 0;byte < bytesNumber;byte ++)
  108. {
  109. R_CSI10_Send_Receive((uint8_t *)&writeData[byte],
  110. 1,
  111. (uint8_t *)readData);
  112. while(CSIIF10 == 0);
  113. }
  114. if(chipSelect == 1)
  115. {
  116. ADI_PART_CS_HIGH;
  117. }
  118. if(chipSelect == 2)
  119. {
  120. ST7579_CS_HIGH;
  121. }
  122. return(bytesNumber);
  123. }
  124. /***************************************************************************//**
  125. * @brief Reads data from SPI.
  126. *
  127. * @param data - As an input parameter, data represents the write buffer:
  128. * - first byte is the chip select number;
  129. * - from the second byte onwards are located data bytes to write.
  130. * As an output parameter, data represents the read buffer:
  131. * - from the first byte onwards are located the read data bytes.
  132. * @param bytesNumber - Number of bytes to write.
  133. *
  134. * @return Number of written bytes.
  135. *******************************************************************************/
  136. unsigned char SPI_Read(unsigned char* data,
  137. unsigned char bytesNumber)
  138. {
  139. unsigned char chipSelect = data[0];
  140. unsigned char writeData[4] = {0, 0, 0, 0};
  141. unsigned char readData[4] = {0, 0, 0, 0};
  142. unsigned char byte = 0;
  143. for(byte = 0;byte < bytesNumber;byte ++)
  144. {
  145. writeData[byte] = data[byte + 1];
  146. data[byte + 1] = 0;
  147. }
  148. if(chipSelect == 1)
  149. {
  150. ADI_PART_CS_LOW;
  151. }
  152. if(chipSelect == 2)
  153. {
  154. ST7579_CS_LOW;
  155. }
  156. for(byte = 0;byte < bytesNumber;byte ++)
  157. {
  158. R_CSI10_Send_Receive((uint8_t *)&writeData[byte],
  159. 1,
  160. (uint8_t *)&readData[byte]);
  161. while(CSIIF10 == 0);
  162. }
  163. if(chipSelect == 1)
  164. {
  165. ADI_PART_CS_HIGH;
  166. }
  167. if(chipSelect == 2)
  168. {
  169. ST7579_CS_HIGH;
  170. }
  171. for(byte = 0;byte < bytesNumber;byte ++)
  172. {
  173. data[byte] = readData[byte];
  174. }
  175. return(bytesNumber);
  176. }