drv_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 zhaohaisheng copy from sch and do some change
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #ifdef BSP_USING_SPI
  14. #include "drv_spi.h"
  15. #include <string.h>
  16. #define DRV_DEBUG
  17. #define LOG_TAG "drv.spi"
  18. #include <drv_log.h>
  19. enum
  20. {
  21. #ifdef BSP_USING_SPI1
  22. SPI1_INDEX,
  23. #endif
  24. #ifdef BSP_USING_SPI2
  25. SPI2_INDEX,
  26. #endif
  27. #ifdef BSP_USING_SPI3
  28. SPI3_INDEX,
  29. #endif
  30. };
  31. static struct ch32_spi_config spi_config[] =
  32. {
  33. #ifdef BSP_USING_SPI1
  34. { \
  35. .Instance = SPI1, \
  36. .bus_name = "spi1", \
  37. .irq_type = SPI1_IRQn, \
  38. },
  39. #endif
  40. #ifdef BSP_USING_SPI2
  41. { \
  42. .Instance = SPI2, \
  43. .bus_name = "spi2", \
  44. .irq_type = SPI2_IRQn, \
  45. },
  46. #endif
  47. #ifdef BSP_USING_SPI3
  48. { \
  49. .Instance = SPI3, \
  50. .bus_name = "spi3", \
  51. .irq_type = SPI3_IRQn, \
  52. }
  53. #endif
  54. };
  55. static struct ch32_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])] = {0};
  56. static rt_uint32_t ch32_spi_clock_get(SPI_TypeDef *spix);
  57. static void ch32_spi_clock_and_io_init(SPI_TypeDef *spix);
  58. static rt_uint8_t spix_readwritebyte(SPI_TypeDef *Instance, rt_uint8_t TxData);
  59. static rt_err_t spi_transmitreceive(SPI_TypeDef *Instance, rt_uint8_t *send_buf, rt_uint8_t *recv_buf, rt_uint16_t send_length);
  60. static rt_err_t spi_transmit(SPI_TypeDef *Instance, rt_uint8_t *send_buf, rt_uint16_t send_length);
  61. static rt_err_t spi_receive(SPI_TypeDef *Instance, rt_uint8_t *recv_buf,rt_uint16_t send_length);
  62. static void ch32_spi_clock_and_io_init(SPI_TypeDef *spix)
  63. {
  64. GPIO_InitTypeDef GPIO_InitStructure;
  65. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  66. if (spix == SPI1)
  67. {
  68. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1|RCC_APB2Periph_GPIOA, ENABLE );
  69. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  70. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  71. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  72. GPIO_Init( GPIOA, &GPIO_InitStructure );
  73. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  75. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76. GPIO_Init( GPIOA, &GPIO_InitStructure );
  77. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  78. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  79. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  80. GPIO_Init( GPIOA, &GPIO_InitStructure );
  81. }
  82. if (spix == SPI2)
  83. {
  84. RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
  85. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
  86. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  87. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  88. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89. GPIO_Init( GPIOB, &GPIO_InitStructure );
  90. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  91. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  92. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  93. GPIO_Init( GPIOB, &GPIO_InitStructure );
  94. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  95. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  96. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  97. GPIO_Init( GPIOB, &GPIO_InitStructure );
  98. }
  99. if (spix == SPI3)
  100. {
  101. RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI3, ENABLE );
  102. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
  103. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  104. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  105. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  106. GPIO_Init( GPIOB, &GPIO_InitStructure );
  107. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  108. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  109. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  110. GPIO_Init( GPIOB, &GPIO_InitStructure );
  111. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  112. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  113. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  114. GPIO_Init( GPIOB, &GPIO_InitStructure );
  115. }
  116. }
  117. static rt_uint32_t ch32_spi_clock_get(SPI_TypeDef *spix)
  118. {
  119. RCC_ClocksTypeDef RCC_Clocks;
  120. RCC_GetClocksFreq(&RCC_Clocks);
  121. if (spix == SPI1)
  122. {
  123. return RCC_Clocks.PCLK2_Frequency;
  124. }
  125. if (spix == SPI2)
  126. {
  127. return RCC_Clocks.PCLK1_Frequency;
  128. }
  129. if (spix == SPI3)
  130. {
  131. return RCC_Clocks.PCLK1_Frequency;
  132. }
  133. return RCC_Clocks.PCLK2_Frequency;
  134. }
  135. /*
  136. *spix read write byte
  137. * */
  138. static rt_uint8_t spix_readwritebyte(SPI_TypeDef *Instance, rt_uint8_t TxData)
  139. {
  140. rt_uint8_t i=0;
  141. while (SPI_I2S_GetFlagStatus(Instance, SPI_I2S_FLAG_TXE) == RESET)
  142. {
  143. i++;
  144. if (i > 200) return 0;
  145. }
  146. SPI_I2S_SendData(Instance, TxData);
  147. i=0;
  148. while (SPI_I2S_GetFlagStatus(Instance, SPI_I2S_FLAG_RXNE) == RESET)
  149. {
  150. i++;
  151. if(i > 200) return 0;
  152. }
  153. return SPI_I2S_ReceiveData(Instance);
  154. }
  155. /*
  156. *spi transmit and receive
  157. * */
  158. static rt_err_t spi_transmitreceive(SPI_TypeDef *Instance, rt_uint8_t *send_buf, rt_uint8_t *recv_buf, rt_uint16_t send_length)
  159. {
  160. rt_uint16_t i=0;
  161. for(i = 0; i < send_length; i++)
  162. {
  163. recv_buf[i] = spix_readwritebyte(Instance, send_buf[i]);
  164. }
  165. return RT_EOK;
  166. }
  167. /*
  168. *spi transmit
  169. * */
  170. static rt_err_t spi_transmit(SPI_TypeDef *Instance, rt_uint8_t *send_buf, rt_uint16_t send_length)
  171. {
  172. rt_uint16_t i=0;
  173. for(i = 0; i < send_length; i++)
  174. {
  175. spix_readwritebyte(Instance, send_buf[i]);
  176. }
  177. return RT_EOK;
  178. }
  179. /*
  180. *spi receive
  181. * */
  182. static rt_err_t spi_receive(SPI_TypeDef *Instance, rt_uint8_t *recv_buf,rt_uint16_t send_length)
  183. {
  184. rt_uint16_t i=0;
  185. for(i = 0; i < send_length; i++)
  186. {
  187. recv_buf[i] = spix_readwritebyte(Instance, 0xFF); /*发送数据为0xff 此时显示为不发送*/
  188. }
  189. return RT_EOK;
  190. }
  191. static rt_err_t ch32_spi_init(struct ch32_spi *spi_drv, struct rt_spi_configuration *cfg)
  192. {
  193. RT_ASSERT(spi_drv != RT_NULL);
  194. RT_ASSERT(cfg != RT_NULL);
  195. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  196. if (cfg->mode & RT_SPI_SLAVE)
  197. {
  198. spi_handle->Init.SPI_Mode = SPI_Mode_Slave;
  199. }
  200. else
  201. {
  202. spi_handle->Init.SPI_Mode = SPI_Mode_Master;
  203. }
  204. if (cfg->mode & RT_SPI_3WIRE)
  205. {
  206. spi_handle->Init.SPI_Direction = SPI_Direction_1Line_Rx;
  207. }
  208. else
  209. {
  210. spi_handle->Init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  211. }
  212. if (cfg->data_width <= 8)
  213. {
  214. spi_handle->Init.SPI_DataSize = SPI_DataSize_8b;
  215. spi_handle->TxXferSize = 8;
  216. spi_handle->RxXferSize = 8;
  217. }
  218. else if (cfg->data_width <= 16)
  219. {
  220. spi_handle->Init.SPI_DataSize = SPI_DataSize_16b;
  221. }
  222. else
  223. {
  224. return -RT_EIO;
  225. }
  226. if (cfg->mode & RT_SPI_CPHA)
  227. {
  228. spi_handle->Init.SPI_CPHA = SPI_CPHA_2Edge;
  229. }
  230. else
  231. {
  232. spi_handle->Init.SPI_CPHA = SPI_CPHA_1Edge;
  233. }
  234. if (cfg->mode & RT_SPI_CPOL)
  235. {
  236. spi_handle->Init.SPI_CPOL = SPI_CPOL_High;
  237. }
  238. else
  239. {
  240. spi_handle->Init.SPI_CPOL = SPI_CPOL_Low;
  241. }
  242. spi_handle->Init.SPI_NSS = SPI_NSS_Soft;
  243. //device is not RT_NULL, so spi_bus not need check
  244. rt_uint32_t SPI_APB_CLOCK;
  245. ch32_spi_clock_and_io_init(spi_handle->Instance);
  246. SPI_APB_CLOCK = ch32_spi_clock_get(spi_handle->Instance);
  247. if (cfg->max_hz >= SPI_APB_CLOCK / 2)
  248. {
  249. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  250. }
  251. else if (cfg->max_hz >= SPI_APB_CLOCK / 4)
  252. {
  253. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  254. }
  255. else if (cfg->max_hz >= SPI_APB_CLOCK / 8)
  256. {
  257. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
  258. }
  259. else if (cfg->max_hz >= SPI_APB_CLOCK / 16)
  260. {
  261. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  262. }
  263. else if (cfg->max_hz >= SPI_APB_CLOCK / 32)
  264. {
  265. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
  266. }
  267. else if (cfg->max_hz >= SPI_APB_CLOCK / 64)
  268. {
  269. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
  270. }
  271. else if (cfg->max_hz >= SPI_APB_CLOCK / 128)
  272. {
  273. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
  274. }
  275. else
  276. {
  277. /* min prescaler 256 */
  278. spi_handle->Init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  279. }
  280. SystemCoreClockUpdate();
  281. LOG_D("sys freq: %d, pclk2 freq: %d, SPI limiting freq: %d, BaudRatePrescaler: %d",
  282. SystemCoreClock,
  283. SPI_APB_CLOCK,
  284. cfg->max_hz,
  285. spi_handle->Init.SPI_BaudRatePrescaler);
  286. if (cfg->mode & RT_SPI_MSB)
  287. {
  288. spi_handle->Init.SPI_FirstBit = SPI_FirstBit_MSB;
  289. }
  290. else
  291. {
  292. spi_handle->Init.SPI_FirstBit = SPI_FirstBit_LSB;
  293. }
  294. SPI_Init(spi_handle->Instance, &spi_handle->Init);
  295. /* Enable SPI_MASTER */
  296. SPI_Cmd(spi_handle->Instance, ENABLE);
  297. LOG_D("%s init done", spi_drv->config->bus_name);
  298. return RT_EOK;
  299. }
  300. static rt_err_t spi_configure(struct rt_spi_device *device,
  301. struct rt_spi_configuration *configuration)
  302. {
  303. RT_ASSERT(device != RT_NULL);
  304. RT_ASSERT(configuration != RT_NULL);
  305. struct ch32_spi *spi_drv = rt_container_of(device->bus, struct ch32_spi, spi_bus);
  306. spi_drv->cfg = configuration;
  307. return ch32_spi_init(spi_drv, configuration);
  308. }
  309. static rt_ssize_t spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  310. {
  311. rt_err_t state = RT_EOK;
  312. rt_size_t message_length, already_send_length;
  313. rt_uint16_t send_length;
  314. rt_uint8_t *recv_buf;
  315. const rt_uint8_t *send_buf;
  316. RT_ASSERT(device != NULL);
  317. RT_ASSERT(device->bus != RT_NULL);
  318. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  319. RT_ASSERT(message != NULL);
  320. struct ch32_spi *spi_drv = rt_container_of(device->bus, struct ch32_spi, spi_bus);
  321. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  322. struct ch32_hw_spi_cs *cs = device->parent.user_data;
  323. /* take CS */
  324. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
  325. {
  326. if (device->config.mode & RT_SPI_CS_HIGH)
  327. GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET);
  328. else
  329. GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_RESET);
  330. }
  331. LOG_D("%s transfer prepare and start", spi_drv->config->bus_name);
  332. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d",
  333. spi_drv->config->bus_name,
  334. (rt_uint32_t)message->send_buf,
  335. (rt_uint32_t)message->recv_buf, message->length);
  336. message_length = message->length;
  337. recv_buf = message->recv_buf;
  338. send_buf = message->send_buf;
  339. while (message_length)
  340. {
  341. /* the HAL library use uint16 to save the data length */
  342. if (message_length > 65535)
  343. {
  344. send_length = 65535;
  345. message_length = message_length - 65535;
  346. }
  347. else
  348. {
  349. send_length = message_length;
  350. message_length = 0;
  351. }
  352. /* calculate the start address */
  353. already_send_length = message->length - send_length - message_length;
  354. /* avoid null pointer problems */
  355. if (message->send_buf)
  356. {
  357. send_buf = (rt_uint8_t *)message->send_buf + already_send_length;
  358. }
  359. if (message->recv_buf)
  360. {
  361. recv_buf = (rt_uint8_t *)message->recv_buf + already_send_length;
  362. }
  363. /* start once data exchange */
  364. if (message->send_buf && message->recv_buf)
  365. {
  366. state = spi_transmitreceive(spi_handle->Instance, (rt_uint8_t *)send_buf, (rt_uint8_t *)recv_buf, send_length);
  367. }
  368. else if (message->send_buf)
  369. {
  370. state = spi_transmit(spi_handle->Instance, (rt_uint8_t *)send_buf, send_length);
  371. if (message->cs_release && (device->config.mode & RT_SPI_3WIRE))
  372. {
  373. /* release the CS by disable SPI when using 3 wires SPI */
  374. SPI_Cmd(spi_handle->Instance, DISABLE);
  375. }
  376. }
  377. else
  378. {
  379. rt_memset((rt_uint8_t *)recv_buf, 0xff, send_length);
  380. /* clear the old error flag */
  381. SPI_I2S_ClearFlag(spi_handle->Instance, SPI_I2S_FLAG_OVR);
  382. state = spi_receive(spi_handle->Instance, (rt_uint8_t *)recv_buf, send_length);
  383. }
  384. if (state != RT_EOK)
  385. {
  386. LOG_I("spi transfer error : %d", state);
  387. message->length = 0;
  388. }
  389. else
  390. {
  391. LOG_D("%s transfer done", spi_drv->config->bus_name);
  392. }
  393. }
  394. /* release CS */
  395. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
  396. {
  397. if (device->config.mode & RT_SPI_CS_HIGH)
  398. GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_RESET);
  399. else
  400. GPIO_WriteBit(cs->GPIOx, cs->GPIO_Pin, Bit_SET);
  401. }
  402. if(state != RT_EOK)
  403. {
  404. return -RT_ERROR;
  405. }
  406. return message->length;
  407. }
  408. static const struct rt_spi_ops ch32_spi_ops =
  409. {
  410. .configure = spi_configure,
  411. .xfer = spi_xfer,
  412. };
  413. static int rt_hw_spi_bus_init(void)
  414. {
  415. rt_err_t result;
  416. for (rt_size_t i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  417. {
  418. spi_bus_obj[i].config = &spi_config[i];
  419. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  420. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  421. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &ch32_spi_ops);
  422. RT_ASSERT(result == RT_EOK);
  423. LOG_D("%s bus init done", spi_config[i].bus_name);
  424. }
  425. return result;
  426. }
  427. /**
  428. * Attach the spi device to SPI bus, this function must be used after initialization.
  429. */
  430. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, rt_uint16_t cs_gpio_pin)
  431. {
  432. RT_ASSERT(bus_name != RT_NULL);
  433. RT_ASSERT(device_name != RT_NULL);
  434. rt_err_t result;
  435. struct rt_spi_device *spi_device;
  436. struct ch32_hw_spi_cs *cs_pin;
  437. /* initialize the cs pin && select the slave*/
  438. GPIO_InitTypeDef GPIO_Initure;
  439. GPIO_Initure.GPIO_Pin = cs_gpio_pin;
  440. GPIO_Initure.GPIO_Mode = GPIO_Mode_Out_PP;
  441. GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz;
  442. GPIO_Init(cs_gpiox, &GPIO_Initure);
  443. GPIO_WriteBit(cs_gpiox, cs_gpio_pin, Bit_SET);
  444. /* attach the device to spi bus*/
  445. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  446. RT_ASSERT(spi_device != RT_NULL);
  447. cs_pin = (struct ch32_hw_spi_cs *)rt_malloc(sizeof(struct ch32_hw_spi_cs));
  448. RT_ASSERT(cs_pin != RT_NULL);
  449. cs_pin->GPIOx = cs_gpiox;
  450. cs_pin->GPIO_Pin = cs_gpio_pin;
  451. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  452. if (result != RT_EOK)
  453. {
  454. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  455. }
  456. RT_ASSERT(result == RT_EOK);
  457. LOG_D("%s attach to %s done", device_name, bus_name);
  458. return result;
  459. }
  460. int rt_hw_spi_init(void)
  461. {
  462. return rt_hw_spi_bus_init();
  463. }
  464. INIT_BOARD_EXPORT(rt_hw_spi_init);
  465. #endif /* BSP_USING_SPI */