drv_spi.c 8.0 KB

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