dvk_spi.c 9.0 KB

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