drv_spi.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * File : drv_spi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-06-05 tanek first implementation.
  13. */
  14. #include "drv_spi.h"
  15. #include <board.h>
  16. #include <finsh.h>
  17. //#define DEBUG
  18. #define ARR_LEN(__N) (sizeof(__N) / sizeof(__N[0]))
  19. #ifdef DEBUG
  20. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  21. #else
  22. #define DEBUG_PRINTF(...)
  23. #endif
  24. /* private rt-thread spi ops function */
  25. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
  26. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message);
  27. static struct rt_spi_ops stm32_spi_ops =
  28. {
  29. configure,
  30. xfer
  31. };
  32. static rt_err_t configure(struct rt_spi_device* device,
  33. struct rt_spi_configuration* configuration)
  34. {
  35. struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
  36. struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)spi_bus->parent.user_data;
  37. spi_parameter_struct spi_init_struct;
  38. uint32_t spi_periph = f4_spi->spi_periph;
  39. RT_ASSERT(device != RT_NULL);
  40. RT_ASSERT(configuration != RT_NULL);
  41. /* data_width */
  42. if(configuration->data_width <= 8)
  43. {
  44. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  45. }
  46. else if(configuration->data_width <= 16)
  47. {
  48. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  49. }
  50. else
  51. {
  52. return RT_EIO;
  53. }
  54. /* baudrate */
  55. {
  56. rcu_clock_freq_enum spi_src;
  57. uint32_t spi_apb_clock;
  58. uint32_t max_hz;
  59. max_hz = configuration->max_hz;
  60. DEBUG_PRINTF("sys freq: %d\n", HAL_RCC_GetSysClockFreq());
  61. DEBUG_PRINTF("pclk2 freq: %d\n", HAL_RCC_GetPCLK2Freq());
  62. DEBUG_PRINTF("max freq: %d\n", max_hz);
  63. if (spi_periph == SPI1 || spi_periph == SPI2)
  64. {
  65. spi_src = CK_APB1;
  66. }
  67. else
  68. {
  69. spi_src = CK_APB2;
  70. }
  71. spi_apb_clock = rcu_clock_freq_get(spi_src);
  72. if(max_hz >= spi_apb_clock/2)
  73. {
  74. spi_init_struct.prescale = SPI_PSC_2;
  75. }
  76. else if (max_hz >= spi_apb_clock/4)
  77. {
  78. spi_init_struct.prescale = SPI_PSC_4;
  79. }
  80. else if (max_hz >= spi_apb_clock/8)
  81. {
  82. spi_init_struct.prescale = SPI_PSC_8;
  83. }
  84. else if (max_hz >= spi_apb_clock/16)
  85. {
  86. spi_init_struct.prescale = SPI_PSC_16;
  87. }
  88. else if (max_hz >= spi_apb_clock/32)
  89. {
  90. spi_init_struct.prescale = SPI_PSC_32;
  91. }
  92. else if (max_hz >= spi_apb_clock/64)
  93. {
  94. spi_init_struct.prescale = SPI_PSC_64;
  95. }
  96. else if (max_hz >= spi_apb_clock/128)
  97. {
  98. spi_init_struct.prescale = SPI_PSC_128;
  99. }
  100. else
  101. {
  102. /* min prescaler 256 */
  103. spi_init_struct.prescale = SPI_PSC_256;
  104. }
  105. } /* baudrate */
  106. switch(configuration->mode)
  107. {
  108. case RT_SPI_MODE_0:
  109. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  110. break;
  111. case RT_SPI_MODE_1:
  112. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;
  113. break;
  114. case RT_SPI_MODE_2:
  115. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE;
  116. break;
  117. case RT_SPI_MODE_3:
  118. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  119. break;
  120. }
  121. /* MSB or LSB */
  122. if(configuration->mode & RT_SPI_MSB)
  123. {
  124. spi_init_struct.endian = SPI_ENDIAN_MSB;
  125. }
  126. else
  127. {
  128. spi_init_struct.endian = SPI_ENDIAN_LSB;
  129. }
  130. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  131. spi_init_struct.device_mode = SPI_MASTER;
  132. spi_init_struct.nss = SPI_NSS_SOFT;
  133. spi_crc_off(spi_periph);
  134. /* init SPI */
  135. spi_init(spi_periph, &spi_init_struct);
  136. /* Enable SPI_MASTER */
  137. spi_enable(spi_periph);
  138. return RT_EOK;
  139. };
  140. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
  141. {
  142. struct rt_spi_bus * stm32_spi_bus = (struct rt_spi_bus *)device->bus;
  143. struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)stm32_spi_bus->parent.user_data;
  144. struct rt_spi_configuration * config = &device->config;
  145. struct stm32_spi_cs * stm32_spi_cs = device->parent.user_data;
  146. uint32_t spi_periph = f4_spi->spi_periph;
  147. RT_ASSERT(device != NULL);
  148. RT_ASSERT(message != NULL);
  149. /* take CS */
  150. if(message->cs_take)
  151. {
  152. gpio_bit_reset(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin);
  153. DEBUG_PRINTF("spi take cs\n");
  154. }
  155. {
  156. if(config->data_width <= 8)
  157. {
  158. const rt_uint8_t * send_ptr = message->send_buf;
  159. rt_uint8_t * recv_ptr = message->recv_buf;
  160. rt_uint32_t size = message->length;
  161. DEBUG_PRINTF("spi poll transfer start: %d\n", size);
  162. while(size--)
  163. {
  164. rt_uint8_t data = 0xFF;
  165. if(send_ptr != RT_NULL)
  166. {
  167. data = *send_ptr++;
  168. }
  169. // Todo: replace register read/write by stm32f4 lib
  170. //Wait until the transmit buffer is empty
  171. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  172. // Send the byte
  173. spi_i2s_data_transmit(spi_periph, data);
  174. //Wait until a data is received
  175. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  176. // Get the received data
  177. data = spi_i2s_data_receive(spi_periph);
  178. if(recv_ptr != RT_NULL)
  179. {
  180. *recv_ptr++ = data;
  181. }
  182. }
  183. DEBUG_PRINTF("spi poll transfer finsh\n");
  184. }
  185. else if(config->data_width <= 16)
  186. {
  187. const rt_uint16_t * send_ptr = message->send_buf;
  188. rt_uint16_t * recv_ptr = message->recv_buf;
  189. rt_uint32_t size = message->length;
  190. while(size--)
  191. {
  192. rt_uint16_t data = 0xFF;
  193. if(send_ptr != RT_NULL)
  194. {
  195. data = *send_ptr++;
  196. }
  197. //Wait until the transmit buffer is empty
  198. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  199. // Send the byte
  200. spi_i2s_data_transmit(spi_periph, data);
  201. //Wait until a data is received
  202. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  203. // Get the received data
  204. data = spi_i2s_data_receive(spi_periph);
  205. if(recv_ptr != RT_NULL)
  206. {
  207. *recv_ptr++ = data;
  208. }
  209. }
  210. }
  211. }
  212. /* release CS */
  213. if(message->cs_release)
  214. {
  215. gpio_bit_set(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin);
  216. DEBUG_PRINTF("spi release cs\n");
  217. }
  218. return message->length;
  219. };
  220. static struct rt_spi_bus spi_bus[];
  221. static const struct stm32f4_spi spis[] = {
  222. #ifdef RT_USING_SPI0
  223. {SPI0, RCU_SPI0, &spi_bus[0]},
  224. #endif
  225. #ifdef RT_USING_SPI1
  226. {SPI1, RCU_SPI1, &spi_bus[1]},
  227. #endif
  228. #ifdef RT_USING_SPI2
  229. {SPI2, RCU_SPI2, &spi_bus[2]},
  230. #endif
  231. #ifdef RT_USING_SPI3
  232. {SPI3, RCU_SPI3, &spi_bus[3]},
  233. #endif
  234. #ifdef RT_USING_SPI4
  235. {SPI4, RCU_SPI4, &spi_bus[4]},
  236. #endif
  237. #ifdef RT_USING_SPI5
  238. {SPI5, RCU_SPI5, &spi_bus[5]},
  239. #endif
  240. };
  241. static struct rt_spi_bus spi_bus[ARR_LEN(spis)];
  242. /** \brief init and register stm32 spi bus.
  243. *
  244. * \param SPI: STM32 SPI, e.g: SPI1,SPI2,SPI3.
  245. * \param spi_bus_name: spi bus name, e.g: "spi1"
  246. * \return
  247. *
  248. */
  249. rt_err_t stm32_spi_bus_register(uint32_t spi_periph,
  250. //struct stm32_spi_bus * stm32_spi,
  251. const char * spi_bus_name)
  252. {
  253. int i;
  254. RT_ASSERT(spi_bus_name != RT_NULL);
  255. for (i = 0; i < ARR_LEN(spis); i++)
  256. {
  257. if (spi_periph == spis[i].spi_periph)
  258. {
  259. rcu_periph_clock_enable(spis[i].spi_clk);
  260. spis[i].spi_bus->parent.user_data = (void *)&spis[i];
  261. rt_spi_bus_register(spis[i].spi_bus, spi_bus_name, &stm32_spi_ops);
  262. return RT_EOK;
  263. }
  264. }
  265. return RT_ERROR;
  266. #ifdef SPI_USE_DMA
  267. /* Configure the DMA handler for Transmission process */
  268. p_spi_bus->hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  269. p_spi_bus->hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  270. //p_spi_bus->hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
  271. p_spi_bus->hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  272. p_spi_bus->hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  273. p_spi_bus->hdma_tx.Init.Mode = DMA_NORMAL;
  274. p_spi_bus->hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
  275. p_spi_bus->hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  276. p_spi_bus->hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  277. p_spi_bus->hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
  278. p_spi_bus->hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  279. p_spi_bus->hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  280. p_spi_bus->hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  281. //p_spi_bus->hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
  282. p_spi_bus->hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  283. p_spi_bus->hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  284. p_spi_bus->hdma_rx.Init.Mode = DMA_NORMAL;
  285. p_spi_bus->hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
  286. p_spi_bus->hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  287. p_spi_bus->hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  288. p_spi_bus->hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
  289. p_spi_bus->hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  290. #endif
  291. }