drv_spi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * 2021-08-23 lianzhian first implementation.
  9. */
  10. #include "drv_spi.h"
  11. #include "gd32f10x.h"
  12. #include <rtthread.h>
  13. #if defined(RT_USING_SPI) && defined(RT_USING_PIN)
  14. #include <rtdevice.h>
  15. #if !defined(RT_USING_SPI0) && !defined(RT_USING_SPI1) && \
  16. !defined(RT_USING_SPI2)
  17. #error "Please define at least one SPIx"
  18. #endif
  19. /* #define DEBUG */
  20. #ifdef DEBUG
  21. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  22. #else
  23. #define DEBUG_PRINTF(...)
  24. #endif
  25. /* private rt-thread spi ops function */
  26. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
  27. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message);
  28. static struct rt_spi_ops gd32_spi_ops =
  29. {
  30. configure,
  31. xfer
  32. };
  33. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)
  34. {
  35. spi_parameter_struct spi_init_struct;
  36. rt_uint32_t spi_periph = (rt_uint32_t)device->bus->parent.user_data;
  37. RT_ASSERT(device != RT_NULL);
  38. RT_ASSERT(configuration != RT_NULL);
  39. if(configuration->data_width <= 8)
  40. {
  41. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  42. }
  43. else if(configuration->data_width <= 16)
  44. {
  45. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  46. }
  47. else
  48. {
  49. return RT_EIO;
  50. }
  51. {
  52. rcu_clock_freq_enum spi_src;
  53. rt_uint32_t spi_apb_clock;
  54. rt_uint32_t max_hz;
  55. max_hz = configuration->max_hz;
  56. DEBUG_PRINTF("sys freq: %d\n", rcu_clock_freq_get(CK_SYS));
  57. DEBUG_PRINTF("CK_APB2 freq: %d\n", rcu_clock_freq_get(CK_APB2));
  58. DEBUG_PRINTF("max freq: %d\n", max_hz);
  59. if (spi_periph == SPI1 || spi_periph == SPI2)
  60. {
  61. spi_src = CK_APB1;
  62. }
  63. else
  64. {
  65. spi_src = CK_APB2;
  66. }
  67. spi_apb_clock = rcu_clock_freq_get(spi_src);
  68. if(max_hz >= spi_apb_clock/2)
  69. {
  70. spi_init_struct.prescale = SPI_PSC_2;
  71. }
  72. else if (max_hz >= spi_apb_clock/4)
  73. {
  74. spi_init_struct.prescale = SPI_PSC_4;
  75. }
  76. else if (max_hz >= spi_apb_clock/8)
  77. {
  78. spi_init_struct.prescale = SPI_PSC_8;
  79. }
  80. else if (max_hz >= spi_apb_clock/16)
  81. {
  82. spi_init_struct.prescale = SPI_PSC_16;
  83. }
  84. else if (max_hz >= spi_apb_clock/32)
  85. {
  86. spi_init_struct.prescale = SPI_PSC_32;
  87. }
  88. else if (max_hz >= spi_apb_clock/64)
  89. {
  90. spi_init_struct.prescale = SPI_PSC_64;
  91. }
  92. else if (max_hz >= spi_apb_clock/128)
  93. {
  94. spi_init_struct.prescale = SPI_PSC_128;
  95. }
  96. else
  97. {
  98. /* min prescaler 256 */
  99. spi_init_struct.prescale = SPI_PSC_256;
  100. }
  101. } /* baudrate */
  102. switch(configuration->mode & RT_SPI_MODE_3)
  103. {
  104. case RT_SPI_MODE_0:
  105. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  106. break;
  107. case RT_SPI_MODE_1:
  108. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;
  109. break;
  110. case RT_SPI_MODE_2:
  111. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE;
  112. break;
  113. case RT_SPI_MODE_3:
  114. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  115. break;
  116. }
  117. /* MSB or LSB */
  118. if(configuration->mode & RT_SPI_MSB)
  119. {
  120. spi_init_struct.endian = SPI_ENDIAN_MSB;
  121. }
  122. else
  123. {
  124. spi_init_struct.endian = SPI_ENDIAN_LSB;
  125. }
  126. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  127. spi_init_struct.device_mode = SPI_MASTER;
  128. spi_init_struct.nss = SPI_NSS_SOFT;
  129. spi_init(spi_periph, &spi_init_struct);
  130. spi_crc_off(spi_periph);
  131. spi_enable(spi_periph);
  132. return RT_EOK;
  133. };
  134. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
  135. {
  136. rt_base_t gd32_cs_pin = (rt_base_t)device->parent.user_data;
  137. rt_uint32_t spi_periph = (rt_uint32_t)device->bus->parent.user_data;
  138. struct rt_spi_configuration * config = &device->config;
  139. RT_ASSERT(device != NULL);
  140. RT_ASSERT(message != NULL);
  141. /* take CS */
  142. if(message->cs_take)
  143. {
  144. rt_pin_write(gd32_cs_pin, PIN_LOW);
  145. DEBUG_PRINTF("spi take cs\n");
  146. }
  147. {
  148. if(config->data_width <= 8)
  149. {
  150. const rt_uint8_t * send_ptr = message->send_buf;
  151. rt_uint8_t * recv_ptr = message->recv_buf;
  152. rt_uint32_t size = message->length;
  153. DEBUG_PRINTF("spi poll transfer start: %d\n", size);
  154. while(size--)
  155. {
  156. rt_uint8_t data = 0xFF;
  157. if(send_ptr != RT_NULL)
  158. {
  159. data = *send_ptr++;
  160. }
  161. /* Todo: replace register read/write by gd32f3 lib */
  162. /* Wait until the transmit buffer is empty */
  163. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  164. /* Send the byte */
  165. spi_i2s_data_transmit(spi_periph, data);
  166. /* Wait until a data is received */
  167. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  168. /* Get the received data */
  169. data = spi_i2s_data_receive(spi_periph);
  170. if(recv_ptr != RT_NULL)
  171. {
  172. *recv_ptr++ = data;
  173. }
  174. }
  175. DEBUG_PRINTF("spi poll transfer finsh\n");
  176. }
  177. else if(config->data_width <= 16)
  178. {
  179. const rt_uint16_t * send_ptr = message->send_buf;
  180. rt_uint16_t * recv_ptr = message->recv_buf;
  181. rt_uint32_t size = message->length;
  182. while(size--)
  183. {
  184. rt_uint16_t data = 0xFF;
  185. if(send_ptr != RT_NULL)
  186. {
  187. data = *send_ptr++;
  188. }
  189. /*Wait until the transmit buffer is empty */
  190. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  191. /* Send the byte */
  192. spi_i2s_data_transmit(spi_periph, data);
  193. /*Wait until a data is received */
  194. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  195. /* Get the received data */
  196. data = spi_i2s_data_receive(spi_periph);
  197. if(recv_ptr != RT_NULL)
  198. {
  199. *recv_ptr++ = data;
  200. }
  201. }
  202. }
  203. }
  204. /* release CS */
  205. if(message->cs_release)
  206. {
  207. rt_pin_write(gd32_cs_pin, PIN_HIGH);
  208. DEBUG_PRINTF("spi release cs\n");
  209. }
  210. return message->length;
  211. };
  212. int gd32_hw_spi_init(void)
  213. {
  214. int result = 0;
  215. #ifdef RT_USING_SPI0
  216. static struct rt_spi_bus spi_bus0;
  217. spi_bus0.parent.user_data = (void *)SPI0;
  218. result = rt_spi_bus_register(&spi_bus0, "spi0", &gd32_spi_ops);
  219. rcu_periph_clock_enable(RCU_GPIOA);
  220. rcu_periph_clock_enable(RCU_SPI0);
  221. /* SPI0_SCK(PA5), SPI0_MISO(PA6) and SPI0_MOSI(PA7) GPIO pin configuration */
  222. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_7);
  223. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
  224. #endif
  225. #ifdef RT_USING_SPI1
  226. static struct rt_spi_bus spi_bus1;
  227. spi_bus1.parent.user_data = (void *)SPI1;
  228. result = rt_spi_bus_register(&spi_bus1, "spi1", &gd32_spi_ops);
  229. rcu_periph_clock_enable(RCU_SPI1);
  230. rcu_periph_clock_enable(RCU_GPIOB);
  231. /* SPI1_SCK(PB13), SPI1_MISO(PB14) and SPI1_MOSI(PB15) GPIO pin configuration */
  232. gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_15);
  233. gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
  234. #endif
  235. #ifdef RT_USING_SPI2
  236. static struct rt_spi_bus spi_bus2;
  237. spi_bus2.parent.user_data = (void *)SPI2;
  238. result = rt_spi_bus_register(&spi_bus2, "spi2", &gd32_spi_ops);
  239. rcu_periph_clock_enable(RCU_SPI2);
  240. rcu_periph_clock_enable(RCU_GPIOB);
  241. /* SPI2_SCK(PB3), SPI2_MISO(PB4) and SPI2_MOSI(PB5) GPIO pin configuration */
  242. gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5);
  243. gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
  244. #endif
  245. return result;
  246. }
  247. INIT_BOARD_EXPORT(gd32_hw_spi_init);
  248. #endif