drv_spi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-20 BruceOu first implementation
  9. */
  10. #include "drv_spi.h"
  11. #ifdef RT_USING_SPI
  12. #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
  13. #define LOG_TAG "drv.spi"
  14. #include <rtdbg.h>
  15. #ifdef BSP_USING_SPI0
  16. static struct rt_spi_bus spi_bus0;
  17. #endif
  18. #ifdef BSP_USING_SPI1
  19. static struct rt_spi_bus spi_bus1;
  20. #endif
  21. #ifdef BSP_USING_SPI2
  22. static struct rt_spi_bus spi_bus2;
  23. #endif
  24. static const struct gd32_spi spi_bus_obj[] = {
  25. #ifdef BSP_USING_SPI0
  26. {
  27. SPI0,
  28. "spi0",
  29. RCU_SPI0,
  30. RCU_GPIOA,
  31. &spi_bus0,
  32. GPIOA,
  33. GPIO_PIN_5,
  34. GPIO_PIN_6,
  35. GPIO_PIN_7,
  36. }
  37. #endif /* BSP_USING_SPI0 */
  38. #ifdef BSP_USING_SPI1
  39. {
  40. SPI1,
  41. "spi1",
  42. RCU_SPI1,
  43. RCU_GPIOB,
  44. &spi_bus1,
  45. GPIOB,
  46. GPIO_PIN_12,
  47. GPIO_PIN_14,
  48. GPIO_PIN_15,
  49. }
  50. #endif /* BSP_USING_SPI1 */
  51. #ifdef BSP_USING_SPI2
  52. {
  53. SPI2,
  54. "spi2",
  55. RCU_SPI2,
  56. RCU_GPIOB,
  57. &spi_bus2,
  58. GPIOB,
  59. GPIO_PIN_3,
  60. GPIO_PIN_4,
  61. GPIO_PIN_5,
  62. }
  63. #endif /* BSP_USING_SPI2 */
  64. };
  65. /* private rt-thread spi ops function */
  66. static rt_err_t spi_configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
  67. static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message* message);
  68. static struct rt_spi_ops gd32_spi_ops =
  69. {
  70. .configure = spi_configure,
  71. .xfer = spixfer,
  72. };
  73. /**
  74. * @brief SPI Initialization
  75. * @param gd32_spi: SPI BUS
  76. * @retval None
  77. */
  78. static void gd32_spi_init(struct gd32_spi *gd32_spi)
  79. {
  80. /* enable SPI clock */
  81. rcu_periph_clock_enable(gd32_spi->spi_clk);
  82. rcu_periph_clock_enable(gd32_spi->gpio_clk);
  83. #if defined SOC_SERIES_GD32F4xx
  84. /*GPIO pin configuration*/
  85. gpio_af_set(gd32_spi->spi_port, GPIO_AF_5, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
  86. gpio_mode_set(gd32_spi->spi_port, GPIO_MODE_AF, GPIO_PUPD_NONE, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
  87. gpio_output_options_set(gd32_spi->spi_port, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
  88. #else
  89. /* Init SPI SCK MOSI */
  90. gpio_init(gd32_spi->spi_port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin);
  91. /* Init SPI MISO */
  92. gpio_init(gd32_spi->spi_port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, gd32_spi->miso_pin);
  93. #endif
  94. }
  95. static rt_err_t spi_configure(struct rt_spi_device* device,
  96. struct rt_spi_configuration* configuration)
  97. {
  98. struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
  99. struct gd32_spi *spi_device = (struct gd32_spi *)spi_bus->parent.user_data;
  100. spi_parameter_struct spi_init_struct;
  101. uint32_t spi_periph = spi_device->spi_periph;
  102. RT_ASSERT(device != RT_NULL);
  103. RT_ASSERT(configuration != RT_NULL);
  104. //Init SPI
  105. gd32_spi_init(spi_device);
  106. /* data_width */
  107. if(configuration->data_width <= 8)
  108. {
  109. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  110. }
  111. else if(configuration->data_width <= 16)
  112. {
  113. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  114. }
  115. else
  116. {
  117. return -RT_EIO;
  118. }
  119. /* baudrate */
  120. {
  121. rcu_clock_freq_enum spi_src;
  122. uint32_t spi_apb_clock;
  123. uint32_t max_hz;
  124. max_hz = configuration->max_hz;
  125. LOG_D("sys freq: %d\n", rcu_clock_freq_get(CK_SYS));
  126. LOG_D("CK_APB2 freq: %d\n", rcu_clock_freq_get(CK_APB2));
  127. LOG_D("max freq: %d\n", max_hz);
  128. if (spi_periph == SPI1 || spi_periph == SPI2)
  129. {
  130. spi_src = CK_APB1;
  131. }
  132. else
  133. {
  134. spi_src = CK_APB2;
  135. }
  136. spi_apb_clock = rcu_clock_freq_get(spi_src);
  137. if(max_hz >= spi_apb_clock/2)
  138. {
  139. spi_init_struct.prescale = SPI_PSC_2;
  140. }
  141. else if (max_hz >= spi_apb_clock/4)
  142. {
  143. spi_init_struct.prescale = SPI_PSC_4;
  144. }
  145. else if (max_hz >= spi_apb_clock/8)
  146. {
  147. spi_init_struct.prescale = SPI_PSC_8;
  148. }
  149. else if (max_hz >= spi_apb_clock/16)
  150. {
  151. spi_init_struct.prescale = SPI_PSC_16;
  152. }
  153. else if (max_hz >= spi_apb_clock/32)
  154. {
  155. spi_init_struct.prescale = SPI_PSC_32;
  156. }
  157. else if (max_hz >= spi_apb_clock/64)
  158. {
  159. spi_init_struct.prescale = SPI_PSC_64;
  160. }
  161. else if (max_hz >= spi_apb_clock/128)
  162. {
  163. spi_init_struct.prescale = SPI_PSC_128;
  164. }
  165. else
  166. {
  167. /* min prescaler 256 */
  168. spi_init_struct.prescale = SPI_PSC_256;
  169. }
  170. } /* baudrate */
  171. switch(configuration->mode & RT_SPI_MODE_3)
  172. {
  173. case RT_SPI_MODE_0:
  174. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  175. break;
  176. case RT_SPI_MODE_1:
  177. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;
  178. break;
  179. case RT_SPI_MODE_2:
  180. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE;
  181. break;
  182. case RT_SPI_MODE_3:
  183. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  184. break;
  185. }
  186. /* MSB or LSB */
  187. if(configuration->mode & RT_SPI_MSB)
  188. {
  189. spi_init_struct.endian = SPI_ENDIAN_MSB;
  190. }
  191. else
  192. {
  193. spi_init_struct.endian = SPI_ENDIAN_LSB;
  194. }
  195. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  196. spi_init_struct.device_mode = SPI_MASTER;
  197. spi_init_struct.nss = SPI_NSS_SOFT;
  198. spi_crc_off(spi_periph);
  199. /* init SPI */
  200. spi_init(spi_periph, &spi_init_struct);
  201. /* Enable SPI_MASTER */
  202. spi_enable(spi_periph);
  203. return RT_EOK;
  204. };
  205. static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message* message)
  206. {
  207. struct rt_spi_bus * gd32_spi_bus = (struct rt_spi_bus *)device->bus;
  208. struct gd32_spi *spi_device = (struct gd32_spi *)gd32_spi_bus->parent.user_data;
  209. struct rt_spi_configuration * config = &device->config;
  210. struct gd32_spi_cs * gd32_spi_cs = device->parent.user_data;
  211. uint32_t spi_periph = spi_device->spi_periph;
  212. RT_ASSERT(device != NULL);
  213. RT_ASSERT(message != NULL);
  214. /* take CS */
  215. if(message->cs_take)
  216. {
  217. gpio_bit_reset(gd32_spi_cs->GPIOx, gd32_spi_cs->GPIO_Pin);
  218. LOG_D("spi take cs\n");
  219. }
  220. {
  221. if(config->data_width <= 8)
  222. {
  223. const rt_uint8_t * send_ptr = message->send_buf;
  224. rt_uint8_t * recv_ptr = message->recv_buf;
  225. rt_uint32_t size = message->length;
  226. LOG_D("spi poll transfer start: %d\n", size);
  227. while(size--)
  228. {
  229. rt_uint8_t data = 0xFF;
  230. if(send_ptr != RT_NULL)
  231. {
  232. data = *send_ptr++;
  233. }
  234. // Todo: replace register read/write by gd32f4 lib
  235. //Wait until the transmit buffer is empty
  236. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  237. // Send the byte
  238. spi_i2s_data_transmit(spi_periph, data);
  239. //Wait until a data is received
  240. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  241. // Get the received data
  242. data = spi_i2s_data_receive(spi_periph);
  243. if(recv_ptr != RT_NULL)
  244. {
  245. *recv_ptr++ = data;
  246. }
  247. }
  248. LOG_D("spi poll transfer finsh\n");
  249. }
  250. else if(config->data_width <= 16)
  251. {
  252. const rt_uint16_t * send_ptr = message->send_buf;
  253. rt_uint16_t * recv_ptr = message->recv_buf;
  254. rt_uint32_t size = message->length;
  255. while(size--)
  256. {
  257. rt_uint16_t data = 0xFF;
  258. if(send_ptr != RT_NULL)
  259. {
  260. data = *send_ptr++;
  261. }
  262. //Wait until the transmit buffer is empty
  263. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  264. // Send the byte
  265. spi_i2s_data_transmit(spi_periph, data);
  266. //Wait until a data is received
  267. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  268. // Get the received data
  269. data = spi_i2s_data_receive(spi_periph);
  270. if(recv_ptr != RT_NULL)
  271. {
  272. *recv_ptr++ = data;
  273. }
  274. }
  275. }
  276. }
  277. /* release CS */
  278. if(message->cs_release)
  279. {
  280. gpio_bit_set(gd32_spi_cs->GPIOx, gd32_spi_cs->GPIO_Pin);
  281. LOG_D("spi release cs\n");
  282. }
  283. return message->length;
  284. };
  285. int rt_hw_spi_init(void)
  286. {
  287. int result = 0;
  288. int i;
  289. for (i = 0; i < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); i++)
  290. {
  291. spi_bus_obj[i].spi_bus->parent.user_data = (void *)&spi_bus_obj[i];
  292. result = rt_spi_bus_register(spi_bus_obj[i].spi_bus, spi_bus_obj[i].bus_name, &gd32_spi_ops);
  293. RT_ASSERT(result == RT_EOK);
  294. LOG_D("%s bus init done", spi_bus_obj[i].bus_name);
  295. }
  296. return result;
  297. }
  298. INIT_BOARD_EXPORT(rt_hw_spi_init);
  299. #endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
  300. #endif /* RT_USING_SPI */