drv_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2006-2025, 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) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4)
  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. #ifdef BSP_USING_SPI3
  25. static struct rt_spi_bus spi_bus3;
  26. #endif
  27. #ifdef BSP_USING_SPI4
  28. static struct rt_spi_bus spi_bus4;
  29. #endif
  30. static const struct gd32_spi spi_bus_obj[] = {
  31. #ifdef BSP_USING_SPI0
  32. {
  33. SPI0,
  34. "spi0",
  35. RCU_SPI0,
  36. RCU_GPIOA,
  37. &spi_bus0,
  38. GPIOA,
  39. #if defined SOC_SERIES_GD32F4xx
  40. GPIO_AF_5,
  41. #endif
  42. #if defined SOC_SERIES_GD32E23x
  43. GPIO_AF_0,
  44. #endif
  45. GPIO_PIN_5,
  46. GPIO_PIN_6,
  47. GPIO_PIN_7,
  48. },
  49. #endif /* BSP_USING_SPI0 */
  50. #ifdef BSP_USING_SPI1
  51. {
  52. SPI1,
  53. "spi1",
  54. RCU_SPI1,
  55. RCU_GPIOB,
  56. &spi_bus1,
  57. GPIOB,
  58. #if defined SOC_SERIES_GD32F4xx
  59. GPIO_AF_5,
  60. #endif
  61. #if defined SOC_SERIES_GD32E23x
  62. GPIO_AF_0,
  63. #endif
  64. #if defined SOC_SERIES_GD32E23x
  65. GPIO_PIN_13,
  66. #else
  67. GPIO_PIN_12,
  68. #endif
  69. GPIO_PIN_14,
  70. GPIO_PIN_15,
  71. },
  72. #endif /* BSP_USING_SPI1 */
  73. #ifdef BSP_USING_SPI2
  74. {
  75. SPI2,
  76. "spi2",
  77. RCU_SPI2,
  78. RCU_GPIOB,
  79. &spi_bus2,
  80. GPIOB,
  81. #if defined SOC_SERIES_GD32F4xx
  82. GPIO_AF_6,
  83. #endif
  84. GPIO_PIN_3,
  85. GPIO_PIN_4,
  86. GPIO_PIN_5,
  87. },
  88. #endif /* BSP_USING_SPI2 */
  89. #ifdef BSP_USING_SPI3
  90. {
  91. SPI3,
  92. "spi3",
  93. RCU_SPI3,
  94. RCU_GPIOE,
  95. &spi_bus3,
  96. GPIOB,
  97. #if defined SOC_SERIES_GD32F4xx
  98. GPIO_AF_5,
  99. #endif
  100. GPIO_PIN_2,
  101. GPIO_PIN_5,
  102. GPIO_PIN_6,
  103. },
  104. #endif /* BSP_USING_SPI3 */
  105. #ifdef BSP_USING_SPI4
  106. {
  107. SPI4,
  108. "spi4",
  109. RCU_SPI4,
  110. RCU_GPIOF,
  111. &spi_bus4,
  112. GPIOF,
  113. #if defined SOC_SERIES_GD32F4xx
  114. GPIO_AF_5,
  115. #endif
  116. GPIO_PIN_7,
  117. GPIO_PIN_8,
  118. GPIO_PIN_9,
  119. }
  120. #endif /* BSP_USING_SPI4 */
  121. };
  122. /* private rt-thread spi ops function */
  123. static rt_err_t spi_configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
  124. static rt_ssize_t spixfer(struct rt_spi_device* device, struct rt_spi_message* message);
  125. static struct rt_spi_ops gd32_spi_ops =
  126. {
  127. .configure = spi_configure,
  128. .xfer = spixfer,
  129. };
  130. /**
  131. * @brief SPI Initialization
  132. * @param gd32_spi: SPI BUS
  133. * @retval None
  134. */
  135. static void gd32_spi_init(struct gd32_spi *gd32_spi)
  136. {
  137. /* enable SPI clock */
  138. rcu_periph_clock_enable(gd32_spi->spi_clk);
  139. rcu_periph_clock_enable(gd32_spi->gpio_clk);
  140. #if defined SOC_SERIES_GD32F4xx || defined SOC_SERIES_GD32E23x
  141. /*GPIO pin configuration*/
  142. gpio_af_set(gd32_spi->spi_port, gd32_spi->alt_func_num, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
  143. 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);
  144. #if defined SOC_SERIES_GD32E23x
  145. gpio_output_options_set(gd32_spi->spi_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
  146. #else
  147. gpio_output_options_set(gd32_spi->spi_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
  148. #endif
  149. #else
  150. /* Init SPI SCK MOSI */
  151. gpio_init(gd32_spi->spi_port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin);
  152. /* Init SPI MISO */
  153. gpio_init(gd32_spi->spi_port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, gd32_spi->miso_pin);
  154. #endif
  155. }
  156. static rt_err_t spi_configure(struct rt_spi_device* device,
  157. struct rt_spi_configuration* configuration)
  158. {
  159. struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
  160. struct gd32_spi *spi_device = (struct gd32_spi *)spi_bus->parent.user_data;
  161. spi_parameter_struct spi_init_struct;
  162. uint32_t spi_periph = spi_device->spi_periph;
  163. RT_ASSERT(device != RT_NULL);
  164. RT_ASSERT(configuration != RT_NULL);
  165. /* Init SPI */
  166. gd32_spi_init(spi_device);
  167. /* data_width */
  168. if(configuration->data_width <= 8)
  169. {
  170. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  171. }
  172. else if(configuration->data_width <= 16)
  173. {
  174. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  175. }
  176. else
  177. {
  178. return -RT_EIO;
  179. }
  180. /* baudrate */
  181. {
  182. rcu_clock_freq_enum spi_src;
  183. uint32_t spi_apb_clock;
  184. uint32_t max_hz;
  185. max_hz = configuration->max_hz;
  186. LOG_D("sys freq: %d\n", rcu_clock_freq_get(CK_SYS));
  187. LOG_D("CK_APB2 freq: %d\n", rcu_clock_freq_get(CK_APB2));
  188. LOG_D("max freq: %d\n", max_hz);
  189. #if defined SOC_SERIES_GD32E23x
  190. spi_src = CK_APB2;
  191. #else
  192. if (spi_periph == SPI1 || spi_periph == SPI2)
  193. {
  194. spi_src = CK_APB1;
  195. }
  196. else
  197. {
  198. spi_src = CK_APB2;
  199. }
  200. #endif
  201. spi_apb_clock = rcu_clock_freq_get(spi_src);
  202. if(max_hz >= spi_apb_clock/2)
  203. {
  204. spi_init_struct.prescale = SPI_PSC_2;
  205. }
  206. else if (max_hz >= spi_apb_clock/4)
  207. {
  208. spi_init_struct.prescale = SPI_PSC_4;
  209. }
  210. else if (max_hz >= spi_apb_clock/8)
  211. {
  212. spi_init_struct.prescale = SPI_PSC_8;
  213. }
  214. else if (max_hz >= spi_apb_clock/16)
  215. {
  216. spi_init_struct.prescale = SPI_PSC_16;
  217. }
  218. else if (max_hz >= spi_apb_clock/32)
  219. {
  220. spi_init_struct.prescale = SPI_PSC_32;
  221. }
  222. else if (max_hz >= spi_apb_clock/64)
  223. {
  224. spi_init_struct.prescale = SPI_PSC_64;
  225. }
  226. else if (max_hz >= spi_apb_clock/128)
  227. {
  228. spi_init_struct.prescale = SPI_PSC_128;
  229. }
  230. else
  231. {
  232. /* min prescaler 256 */
  233. spi_init_struct.prescale = SPI_PSC_256;
  234. }
  235. } /* baudrate */
  236. switch(configuration->mode & RT_SPI_MODE_3)
  237. {
  238. case RT_SPI_MODE_0:
  239. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  240. break;
  241. case RT_SPI_MODE_1:
  242. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;
  243. break;
  244. case RT_SPI_MODE_2:
  245. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE;
  246. break;
  247. case RT_SPI_MODE_3:
  248. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  249. break;
  250. }
  251. /* MSB or LSB */
  252. if(configuration->mode & RT_SPI_MSB)
  253. {
  254. spi_init_struct.endian = SPI_ENDIAN_MSB;
  255. }
  256. else
  257. {
  258. spi_init_struct.endian = SPI_ENDIAN_LSB;
  259. }
  260. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  261. spi_init_struct.device_mode = SPI_MASTER;
  262. spi_init_struct.nss = SPI_NSS_SOFT;
  263. spi_crc_off(spi_periph);
  264. /* init SPI */
  265. spi_init(spi_periph, &spi_init_struct);
  266. /* Enable SPI_MASTER */
  267. spi_enable(spi_periph);
  268. return RT_EOK;
  269. };
  270. static rt_ssize_t spixfer(struct rt_spi_device* device, struct rt_spi_message* message)
  271. {
  272. struct rt_spi_bus * gd32_spi_bus = (struct rt_spi_bus *)device->bus;
  273. struct gd32_spi *spi_device = (struct gd32_spi *)gd32_spi_bus->parent.user_data;
  274. struct rt_spi_configuration * config = &device->config;
  275. rt_base_t cs_pin = (rt_base_t)device->parent.user_data;
  276. uint32_t spi_periph = spi_device->spi_periph;
  277. RT_ASSERT(device != NULL);
  278. RT_ASSERT(message != NULL);
  279. /* take CS */
  280. if(message->cs_take)
  281. {
  282. rt_pin_write(cs_pin, PIN_LOW);
  283. LOG_D("spi take cs\n");
  284. }
  285. {
  286. if(config->data_width <= 8)
  287. {
  288. const rt_uint8_t * send_ptr = message->send_buf;
  289. rt_uint8_t * recv_ptr = message->recv_buf;
  290. rt_uint32_t size = message->length;
  291. LOG_D("spi poll transfer start: %d\n", size);
  292. while(size--)
  293. {
  294. rt_uint8_t data = 0xFF;
  295. if(send_ptr != RT_NULL)
  296. {
  297. data = *send_ptr++;
  298. }
  299. /* Todo: replace register read/write by gd32f4 lib */
  300. /* Wait until the transmit buffer is empty */
  301. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  302. /* Send the byte */
  303. spi_i2s_data_transmit(spi_periph, data);
  304. /* Wait until a data is received */
  305. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  306. /* Get the received data */
  307. data = spi_i2s_data_receive(spi_periph);
  308. if(recv_ptr != RT_NULL)
  309. {
  310. *recv_ptr++ = data;
  311. }
  312. }
  313. LOG_D("spi poll transfer finsh\n");
  314. }
  315. else if(config->data_width <= 16)
  316. {
  317. const rt_uint16_t * send_ptr = message->send_buf;
  318. rt_uint16_t * recv_ptr = message->recv_buf;
  319. rt_uint32_t size = message->length;
  320. while(size--)
  321. {
  322. rt_uint16_t data = 0xFF;
  323. if(send_ptr != RT_NULL)
  324. {
  325. data = *send_ptr++;
  326. }
  327. /* Wait until the transmit buffer is empty */
  328. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  329. /* Send the byte */
  330. spi_i2s_data_transmit(spi_periph, data);
  331. /* Wait until a data is received */
  332. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  333. /* Get the received data */
  334. data = spi_i2s_data_receive(spi_periph);
  335. if(recv_ptr != RT_NULL)
  336. {
  337. *recv_ptr++ = data;
  338. }
  339. }
  340. }
  341. }
  342. /* release CS */
  343. if(message->cs_release)
  344. {
  345. rt_pin_write(cs_pin, PIN_HIGH);
  346. LOG_D("spi release cs\n");
  347. }
  348. return message->length;
  349. };
  350. /**
  351. * Attach the spi device to SPI bus, this function must be used after initialization.
  352. */
  353. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  354. {
  355. RT_ASSERT(bus_name != RT_NULL);
  356. RT_ASSERT(device_name != RT_NULL);
  357. rt_err_t result;
  358. struct rt_spi_device *spi_device;
  359. /* attach the device to spi bus*/
  360. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  361. RT_ASSERT(spi_device != RT_NULL);
  362. if(cs_pin != PIN_NONE)
  363. {
  364. /* initialize the cs pin && select the slave*/
  365. rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
  366. rt_pin_write(cs_pin, PIN_HIGH);
  367. }
  368. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  369. if (result != RT_EOK)
  370. {
  371. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  372. }
  373. RT_ASSERT(result == RT_EOK);
  374. LOG_D("%s attach to %s done", device_name, bus_name);
  375. return result;
  376. }
  377. int rt_hw_spi_init(void)
  378. {
  379. int result = 0;
  380. int i;
  381. for (i = 0; i < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); i++)
  382. {
  383. spi_bus_obj[i].spi_bus->parent.user_data = (void *)&spi_bus_obj[i];
  384. result = rt_spi_bus_register(spi_bus_obj[i].spi_bus, spi_bus_obj[i].bus_name, &gd32_spi_ops);
  385. RT_ASSERT(result == RT_EOK);
  386. LOG_D("%s bus init done", spi_bus_obj[i].bus_name);
  387. }
  388. return result;
  389. }
  390. INIT_BOARD_EXPORT(rt_hw_spi_init);
  391. #endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4*/
  392. #endif /* RT_USING_SPI */