dvk_spi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**************************************************************************//**
  2. * @file
  3. * @brief EFM32GG_DK3750 board support package SPI API implementation
  4. * @author Energy Micro AS
  5. * @version 2.0.1
  6. ******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. * 4. The source and compiled code may only be used on Energy Micro "EFM32"
  21. * microcontrollers and "EFR4" radios.
  22. *
  23. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  24. * obligation to support this Software. Energy Micro AS is providing the
  25. * Software "AS IS", with no express or implied warranties of any kind,
  26. * including, but not limited to, any implied warranties of merchantability
  27. * or fitness for any particular purpose or warranties against infringement
  28. * of any proprietary rights of a third party.
  29. *
  30. * Energy Micro AS will not be liable for any consequential, incidental, or
  31. * special damages, or any other relief, or for any claim by any third party,
  32. * arising from your use of this Software.
  33. *
  34. *****************************************************************************/
  35. /***************************************************************************//**
  36. * @addtogroup BSP
  37. * @{
  38. ******************************************************************************/
  39. #include "efm32.h"
  40. #include "em_gpio.h"
  41. #include "em_usart.h"
  42. #include "em_cmu.h"
  43. #include "dvk.h"
  44. #include "dvk_bcregisters.h"
  45. /* USART used for SPI access */
  46. #define USART_USED USART2 /**< USART used for BC register interface */
  47. #define USART_CLK cmuClock_USART2 /**< Clock for BC register USART */
  48. /* GPIO pins used, please refer to DVK user guide. */
  49. #define PORT_SPI_TX gpioPortC /**< SPI transmit GPIO port */
  50. #define PIN_SPI_TX 2 /**< SPI transmit GPIO pin */
  51. #define PORT_SPI_RX gpioPortC /**< SPI receive GPIO port */
  52. #define PIN_SPI_RX 3 /**< SPI receive GPIO pin */
  53. #define PORT_SPI_CLK gpioPortC /**< SPI clock port */
  54. #define PIN_SPI_CLK 4 /**< SPI clock pin */
  55. #define PORT_SPI_CS gpioPortC /**< SPI Chip Select port */
  56. #define PIN_SPI_CS 5 /**< SPI Chip Select pin */
  57. static volatile const uint16_t *lastAddr = 0; /**< Last register accessed */
  58. /**************************************************************************//**
  59. * @brief Initializes SPI interface for access to board controller
  60. * FPGA registers
  61. *****************************************************************************/
  62. static void SPI_BC_Init(void)
  63. {
  64. USART_InitSync_TypeDef bcinit = USART_INITSYNC_DEFAULT;
  65. /* Enable module clocks */
  66. CMU_ClockEnable(USART_CLK, true);
  67. /* Configure SPI pins */
  68. GPIO_PinModeSet(PORT_SPI_TX, PIN_SPI_TX, gpioModePushPull, 0);
  69. GPIO_PinModeSet(PORT_SPI_RX, PIN_SPI_RX, gpioModeInput, 0);
  70. GPIO_PinModeSet(PORT_SPI_CLK, PIN_SPI_CLK, gpioModePushPull, 0);
  71. /* Keep CS high to not activate slave */
  72. GPIO_PinModeSet(PORT_SPI_CS, PIN_SPI_CS, gpioModePushPull, 1);
  73. /* Configure to use SPI master with manual CS */
  74. /* For now, configure SPI for worst case 48MHz clock in order to work for all */
  75. /* configurations. */
  76. bcinit.refFreq = 48000000;
  77. bcinit.baudrate = 7000000;
  78. /* Initialize USART */
  79. USART_InitSync(USART_USED, &bcinit);
  80. /* Enable pins at default location */
  81. USART_USED->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN;
  82. }
  83. /**************************************************************************//**
  84. * @brief Disables GPIO pins and USART from FPGA register access
  85. *****************************************************************************/
  86. static void SPI_BC_Disable(void)
  87. {
  88. /* Restore and disable USART */
  89. USART_Reset(USART_USED);
  90. GPIO_PinModeSet(PORT_SPI_TX, PIN_SPI_TX, gpioModeDisabled, 0);
  91. GPIO_PinModeSet(PORT_SPI_RX, PIN_SPI_RX, gpioModeDisabled, 0);
  92. GPIO_PinModeSet(PORT_SPI_CLK, PIN_SPI_CLK, gpioModeDisabled, 0);
  93. GPIO_PinModeSet(PORT_SPI_CS, PIN_SPI_CS, gpioModeDisabled, 0);
  94. /* Disable USART clock - we can't disable GPIO or HFPER as we don't know who else
  95. * might be using it */
  96. CMU_ClockEnable(USART_CLK, false);
  97. }
  98. /**************************************************************************//**
  99. * @brief Perform SPI Transfer
  100. * @param addr Register offset, starting at 0
  101. * @param rw 0 on write, 1 on read accesses
  102. * @param data 16-bit data to write into register/dummy data for reads
  103. * @return 16-bit data received from SPI access
  104. *****************************************************************************/
  105. static uint16_t SPI_BC_Access(uint8_t addr, uint8_t rw, uint16_t data)
  106. {
  107. uint16_t tmp;
  108. /* Enable CS */
  109. GPIO_PinOutClear(PORT_SPI_CS, PIN_SPI_CS);
  110. /* Write SPI address MSB */
  111. USART_Tx(USART_USED, (addr & 0x3) | rw << 3);
  112. /* Just ignore data read back */
  113. USART_Rx(USART_USED);
  114. /* Write SPI address LSB */
  115. USART_Tx(USART_USED, data & 0xFF);
  116. tmp = (uint16_t) USART_Rx(USART_USED);
  117. /* SPI data MSB */
  118. USART_Tx(USART_USED, data >> 8);
  119. tmp |= (uint16_t) USART_Rx(USART_USED) << 8;
  120. /* Disable CS */
  121. GPIO_PinOutSet(PORT_SPI_CS, PIN_SPI_CS);
  122. return tmp;
  123. }
  124. /**************************************************************************//**
  125. * @brief Performs SPI write to FPGA register
  126. * @param addr Address of register
  127. * @param data Data to write
  128. *****************************************************************************/
  129. static void SPI_BC_Write(uint8_t addr, uint16_t data)
  130. {
  131. SPI_BC_Access(addr, 0, data);
  132. }
  133. /**************************************************************************//**
  134. * @brief Performs SPI read from FPGA register
  135. * @param addr Address of register
  136. * @return 16-bit value of board controller register
  137. *****************************************************************************/
  138. static uint16_t SPI_BC_Read(uint8_t addr)
  139. {
  140. return SPI_BC_Access(addr, 1, 0);
  141. }
  142. /**************************************************************************//**
  143. * @brief Initializes DVK register access
  144. * @return true on success, false on failure
  145. *****************************************************************************/
  146. bool DVK_SPI_init(void)
  147. {
  148. uint16_t bcMagic;
  149. /* Enable HF and GPIO clocks */
  150. CMU_ClockEnable(cmuClock_HFPER, true);
  151. CMU_ClockEnable(cmuClock_GPIO, true);
  152. /* Configure SPI mode of operation */
  153. DVK_busControlMode(DVK_BusControl_SPI);
  154. SPI_BC_Init();
  155. /* Read "board control Magic" register to verify SPI is up and running */
  156. /* if not FPGA is configured to be in EBI mode */
  157. bcMagic = DVK_SPI_readRegister(&BC_REGISTER->MAGIC);
  158. if (bcMagic != BC_MAGIC_VALUE)
  159. {
  160. return false;
  161. }
  162. else
  163. {
  164. return true;
  165. }
  166. }
  167. /**************************************************************************//**
  168. * @brief Disable and free up resources used by SPI board control access
  169. *****************************************************************************/
  170. void DVK_SPI_disable(void)
  171. {
  172. SPI_BC_Disable();
  173. }
  174. /**************************************************************************//**
  175. * @brief Perform read from DVK board control register
  176. * @param[in] addr Address of register to read from
  177. * @return Value of board controller register
  178. *****************************************************************************/
  179. uint16_t DVK_SPI_readRegister(volatile uint16_t *addr)
  180. {
  181. uint16_t data;
  182. if (addr != lastAddr)
  183. {
  184. SPI_BC_Write(0x00, 0xFFFF & ((uint32_t) addr)); /*LSBs of address*/
  185. SPI_BC_Write(0x01, 0xFF & ((uint32_t) addr >> 16)); /*MSBs of address*/
  186. SPI_BC_Write(0x02, (0x0C000000 & (uint32_t) addr) >> 26); /*Chip select*/
  187. }
  188. /* Read twice; when register address has changed we need two SPI transfer
  189. * to clock out valid data through board controller FIFOs */
  190. data = SPI_BC_Read(0x03);
  191. data = SPI_BC_Read(0x03);
  192. lastAddr = addr;
  193. return data;
  194. }
  195. /**************************************************************************//**
  196. * @brief Perform write to DVK board control register
  197. * @param addr Address of register to write to
  198. * @param data 16-bit to write into register
  199. *****************************************************************************/
  200. void DVK_SPI_writeRegister(volatile uint16_t *addr, uint16_t data)
  201. {
  202. if (addr != lastAddr)
  203. {
  204. SPI_BC_Write(0x00, 0xFFFF & ((uint32_t) addr)); /*LSBs of address*/
  205. SPI_BC_Write(0x01, 0xFF & ((uint32_t) addr >> 16)); /*MSBs of address*/
  206. SPI_BC_Write(0x02, (0x0C000000 & (uint32_t) addr) >> 26); /*Chip select*/
  207. }
  208. SPI_BC_Write(0x03, data); /*Data*/
  209. lastAddr = addr;
  210. }
  211. /** @} (end group BSP) */