drv_spi.c 8.4 KB

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