drv_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-23 yuzrain the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <string.h>
  13. #include <rthw.h>
  14. #include "board.h"
  15. #include "drv_spi.h"
  16. #include "md_spi.h"
  17. #include "md_gpio.h"
  18. #ifdef RT_USING_SPI
  19. #define SPITIMEOUT 0x0FFF
  20. static rt_err_t __spi_send(struct rt_spi_device *device, rt_uint8_t *buf,
  21. rt_int32_t len, rt_uint32_t tmout);
  22. static rt_err_t __spi_recv(struct rt_spi_device *device, rt_uint8_t *buf,
  23. rt_int32_t len, rt_uint32_t tmout);
  24. static rt_err_t __spi_send_recv(struct rt_spi_device *device, rt_uint8_t *tbuf,
  25. rt_uint8_t *rbuf, rt_int32_t len, rt_uint32_t tmout);
  26. /**
  27. * @brief: SPI single line send.
  28. * @param: device, pointer to the SPI device
  29. * @param: buf, send data buffer
  30. * @param: len, the length of buf
  31. * @param: tmout, timeout
  32. * @retval: rt_err_t
  33. */
  34. static rt_err_t __spi_send(struct rt_spi_device *device, rt_uint8_t *buf,
  35. rt_int32_t len, rt_uint32_t tmout)
  36. {
  37. SPI_TypeDef *hspi;
  38. rt_uint32_t rt_timout;
  39. rt_uint8_t temp_data;
  40. /* Get the SPI port */
  41. hspi = (SPI_TypeDef *)device->bus->parent.user_data;
  42. /* Open SPI if it is disabled */
  43. if (READ_BIT(hspi->CON1, SPI_CON1_SPIEN_MSK) != SPI_CON1_SPIEN_MSK)
  44. SET_BIT(hspi->CON1, SPI_CON1_SPIEN_MSK);
  45. while (len > 0)
  46. {
  47. /* Confirm that no data is being transmitted */
  48. rt_timout = tmout;
  49. while (((hspi->STAT & SPI_STAT_TXE_MSK) == 0) && (--rt_timout));
  50. if (rt_timout == 0)
  51. return RT_ETIMEOUT;
  52. /* Send data */
  53. if (device->config.data_width == 8)
  54. {
  55. hspi->DATA = *(rt_uint8_t *)buf;
  56. buf++;
  57. len--;
  58. }
  59. else if (device->config.data_width == 16)
  60. {
  61. hspi->DATA = *(rt_uint16_t *)buf;
  62. buf += 2;
  63. len -= 2;
  64. }
  65. else
  66. return RT_EINVAL;
  67. }
  68. /* At here, we have transmitted all the data.
  69. * The next step is to clear the IT flag.
  70. */
  71. for (rt_uint8_t i = 0; i < md_spi_get_stat_rxflv(hspi); i++)
  72. temp_data = hspi->DATA;
  73. UNUSED(temp_data);
  74. hspi->ICR = hspi->RIF;
  75. return RT_EOK;
  76. }
  77. /**
  78. * @brief: SPI single line receive.
  79. * @param: device, pointer to the SPI device
  80. * @param: buf, receive data buffer
  81. * @param: len, the length of buf
  82. * @param: tmout, timeout
  83. * @retval: rt_err_t
  84. */
  85. static rt_err_t __spi_recv(struct rt_spi_device *device, rt_uint8_t *buf,
  86. rt_int32_t len, rt_uint32_t tmout)
  87. {
  88. SPI_TypeDef *hspi;
  89. rt_uint32_t rt_timout;
  90. /* Get the SPI port */
  91. hspi = (SPI_TypeDef *)device->bus->parent.user_data;
  92. /* Open SPI if it is disabled */
  93. if (READ_BIT(hspi->CON1, SPI_CON1_SPIEN_MSK) != SPI_CON1_SPIEN_MSK)
  94. SET_BIT(hspi->CON1, SPI_CON1_SPIEN_MSK);
  95. /* Handle data in __spi_send_recv() function */
  96. if (((device->config.mode & RT_SPI_SLAVE) == 0)
  97. && ((device->config.mode & RT_SPI_3WIRE) == 0))
  98. __spi_send_recv(device, buf, buf, len, tmout);
  99. while (len > 0)
  100. {
  101. /* Waiting for data */
  102. rt_timout = tmout;
  103. while (((hspi->STAT & SPI_STAT_RXTH_MSK) == 0) && (--rt_timout));
  104. if (rt_timout == 0)
  105. return RT_ETIMEOUT;
  106. /* Send data */
  107. if (device->config.data_width == 8)
  108. {
  109. *(rt_uint8_t *)buf = hspi->DATA;
  110. buf++;
  111. len--;
  112. }
  113. else if (device->config.data_width == 16)
  114. {
  115. *(rt_uint16_t *)buf = hspi->DATA;
  116. buf += 2;
  117. len -= 2;
  118. }
  119. else
  120. return RT_EINVAL;
  121. }
  122. /* At here, we have transmitted all the data.
  123. * The next step is to clear the IT flag.
  124. */
  125. hspi->ICR = hspi->RIF;
  126. return RT_EOK;
  127. }
  128. /**
  129. * @brief: SPI two line transmission.
  130. * @param: device, pointer to the SPI device
  131. * @param: tbuf, send data buffer
  132. * @param: rbuf, receive data buffer
  133. * @param: len, the length of buf
  134. * @param: tmout, timeout
  135. * @retval: rt_err_t
  136. */
  137. static rt_err_t __spi_send_recv(struct rt_spi_device *device, rt_uint8_t *tbuf,
  138. rt_uint8_t *rbuf, rt_int32_t len, rt_uint32_t tmout)
  139. {
  140. SPI_TypeDef *hspi;
  141. rt_uint32_t rt_timout;
  142. /* Get the SPI port */
  143. hspi = (SPI_TypeDef *)device->bus->parent.user_data;
  144. /* Open SPI if it is disabled */
  145. if (READ_BIT(hspi->CON1, SPI_CON1_SPIEN_MSK) != SPI_CON1_SPIEN_MSK)
  146. SET_BIT(hspi->CON1, SPI_CON1_SPIEN_MSK);
  147. /* return error if SPI is in 1-line mode */
  148. if ((device->config.mode & RT_SPI_3WIRE) == RT_SPI_3WIRE)
  149. return RT_ERROR;
  150. while (len > 0)
  151. {
  152. /* Confirm that no data is being transmitted */
  153. rt_timout = tmout;
  154. while (((hspi->STAT & SPI_STAT_TXE_MSK) == 0) && (--rt_timout));
  155. if (rt_timout == 0)
  156. return RT_ETIMEOUT;
  157. /* Send data */
  158. if (device->config.data_width == 8)
  159. {
  160. hspi->DATA = *(rt_uint8_t *)tbuf;
  161. tbuf++;
  162. len--;
  163. }
  164. else if (device->config.data_width == 16)
  165. {
  166. hspi->DATA = *(rt_uint16_t *)tbuf;
  167. tbuf += 2;
  168. len -= 2;
  169. }
  170. else
  171. return RT_EINVAL;
  172. /* Waiting for data */
  173. rt_timout = tmout;
  174. while (((hspi->STAT & SPI_STAT_RXTH_MSK) == 0) && (--rt_timout));
  175. if (rt_timout == 0)
  176. return RT_ETIMEOUT;
  177. /* Send data */
  178. if (device->config.data_width == 8)
  179. {
  180. *(rt_uint8_t *)rbuf = hspi->DATA;
  181. rbuf++;
  182. }
  183. else if (device->config.data_width == 16)
  184. {
  185. *(rt_uint16_t *)rbuf = hspi->DATA;
  186. rbuf += 2;
  187. }
  188. }
  189. /* At here, we have transmitted all the data.
  190. * The next step is to clear the IT flag.
  191. */
  192. hspi->ICR = hspi->RIF;
  193. return RT_EOK;
  194. }
  195. rt_err_t spi_configure(struct rt_spi_device *device,
  196. struct rt_spi_configuration *cfg)
  197. {
  198. SPI_TypeDef *hspi;
  199. hspi = (SPI_TypeDef *)device->bus->parent.user_data;
  200. /* Close SPI temporarily */
  201. md_spi_disable_con1_spien(hspi);
  202. /* config spi mode */
  203. if (cfg->mode & RT_SPI_SLAVE)
  204. md_spi_set_con1_mstren(hspi, MD_SPI_MODE_SLAVE);
  205. else
  206. md_spi_set_con1_mstren(hspi, MD_SPI_MODE_MASTER);
  207. /* Config data mode */
  208. if (cfg->mode & RT_SPI_3WIRE)
  209. md_spi_set_con1_bidimode(hspi, MD_SPI_HALF_DUPLEX);
  210. else
  211. md_spi_set_con1_bidimode(hspi, MD_SPI_FULL_DUPLEX);
  212. /* Config data width */
  213. if (cfg->data_width == 8)
  214. md_spi_set_con1_flen(hspi, MD_SPI_FRAME_FORMAT_8BIT);
  215. else if (cfg->data_width == 16)
  216. md_spi_set_con1_flen(hspi, SPI_CON1_FLEN_MSK);
  217. /* Config phase */
  218. if (cfg->mode & RT_SPI_CPHA)
  219. md_spi_set_con1_cpha(hspi, MD_SPI_PHASE_2EDGE);
  220. else
  221. md_spi_set_con1_cpha(hspi, MD_SPI_PHASE_1EDGE);
  222. /* Config polarity */
  223. if (cfg->mode & RT_SPI_CPOL)
  224. md_spi_set_con1_cpol(hspi, MD_SPI_POLARITY_HIGH);
  225. else
  226. md_spi_set_con1_cpol(hspi, MD_SPI_POLARITY_LOW);
  227. /* Config if NSS pin is managed by software */
  228. md_spi_disable_con1_ssen(hspi);
  229. /* config spi clock */
  230. if (cfg->max_hz >= SystemCoreClock / 2)
  231. {
  232. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  233. if (SystemCoreClock / 2 <= 10000000)
  234. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV2);
  235. else if (SystemCoreClock / 4 <= 10000000)
  236. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV4);
  237. else
  238. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV8);
  239. }
  240. else if (cfg->max_hz >= SystemCoreClock / 4)
  241. {
  242. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  243. if (SystemCoreClock / 4 <= 10000000)
  244. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV4);
  245. else
  246. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV8);
  247. }
  248. else if (cfg->max_hz >= SystemCoreClock / 8)
  249. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV8);
  250. else if (cfg->max_hz >= SystemCoreClock / 16)
  251. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV16);
  252. else if (cfg->max_hz >= SystemCoreClock / 32)
  253. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV32);
  254. else if (cfg->max_hz >= SystemCoreClock / 64)
  255. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV64);
  256. else if (cfg->max_hz >= SystemCoreClock / 128)
  257. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV128);
  258. else
  259. md_spi_set_con1_baud(hspi, MD_SPI_BAUDRATEPRESCALER_DIV256);
  260. /* Enable SPI */
  261. md_spi_enable_con1_spien(hspi);
  262. return RT_EOK;
  263. }
  264. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  265. {
  266. rt_err_t res;
  267. rt_uint32_t *cs;
  268. RT_ASSERT(device != RT_NULL);
  269. RT_ASSERT(device->bus != RT_NULL);
  270. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  271. RT_ASSERT(message->send_buf != RT_NULL || message->recv_buf != RT_NULL);
  272. cs = (rt_uint32_t *)device->parent.user_data;
  273. /* only send data */
  274. if (message->recv_buf == RT_NULL)
  275. {
  276. if (message->cs_take)
  277. {
  278. rt_pin_write(*cs, 0);
  279. }
  280. res = __spi_send(device, (rt_uint8_t *)message->send_buf, (rt_int32_t)message->length, SPITIMEOUT);
  281. if (message->cs_release)
  282. {
  283. rt_pin_write(*cs, 1);
  284. }
  285. if (res != RT_EOK)
  286. return RT_ERROR;
  287. }
  288. /* only receive data */
  289. if (message->send_buf == RT_NULL)
  290. {
  291. if (message->cs_take)
  292. {
  293. rt_pin_write(*cs, 0);
  294. }
  295. res = __spi_recv(device, (rt_uint8_t *)message->recv_buf, (rt_int32_t)message->length, SPITIMEOUT);
  296. if (message->cs_release)
  297. {
  298. rt_pin_write(*cs, 1);
  299. }
  300. if (res != RT_EOK)
  301. return RT_ERROR;
  302. }
  303. /* send & receive */
  304. else
  305. {
  306. if (message->cs_take)
  307. {
  308. rt_pin_write(*cs, 0);
  309. }
  310. res = __spi_send_recv(device, (rt_uint8_t *)message->send_buf, (rt_uint8_t *)message->recv_buf,
  311. (rt_int32_t)message->length, SPITIMEOUT);
  312. if (message->cs_release)
  313. {
  314. rt_pin_write(*cs, 1);
  315. }
  316. if (res != RT_EOK)
  317. return RT_ERROR;
  318. }
  319. return message->length;
  320. }
  321. const struct rt_spi_ops es32f0_spi_ops =
  322. {
  323. spi_configure,
  324. spixfer,
  325. };
  326. static struct rt_spi_bus _spi_bus1, _spi_bus2;
  327. int es32f0_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
  328. {
  329. struct rt_spi_bus *spi_bus;
  330. if (SPIx == SPI2)
  331. {
  332. /* Open GPIO and SPI clock */
  333. SET_BIT(RCU->APB1EN, RCU_APB1EN_SPI2EN_MSK);
  334. SET_BIT(RCU->AHBEN, RCU_AHBEN_GPBEN_MSK);
  335. /* Config SPI2 GPIO */
  336. md_gpio_set_mode (GPIOB, MD_GPIO_PIN_13, MD_GPIO_MODE_FUNCTION);
  337. md_gpio_set_mode (GPIOB, MD_GPIO_PIN_14, MD_GPIO_MODE_FUNCTION);
  338. md_gpio_set_mode (GPIOB, MD_GPIO_PIN_15, MD_GPIO_MODE_FUNCTION);
  339. md_gpio_set_function8_15 (GPIOB, MD_GPIO_PIN_13, MD_GPIO_AF0);
  340. md_gpio_set_function8_15 (GPIOB, MD_GPIO_PIN_14, MD_GPIO_AF0);
  341. md_gpio_set_function8_15 (GPIOB, MD_GPIO_PIN_15, MD_GPIO_AF0);
  342. /* Remember SPI bus2 */
  343. spi_bus = &_spi_bus2;
  344. }
  345. else if (SPIx == SPI1)
  346. {
  347. /* Open GPIO and SPI clock */
  348. SET_BIT(RCU->APB2EN, RCU_APB2EN_SPI1EN_MSK);
  349. SET_BIT(RCU->AHBEN, RCU_AHBEN_GPBEN_MSK);
  350. /* Config SPI1 GPIO */
  351. md_gpio_set_mode (GPIOB, MD_GPIO_PIN_3, MD_GPIO_MODE_FUNCTION);
  352. md_gpio_set_mode (GPIOB, MD_GPIO_PIN_4, MD_GPIO_MODE_FUNCTION);
  353. md_gpio_set_mode (GPIOB, MD_GPIO_PIN_5, MD_GPIO_MODE_FUNCTION);
  354. md_gpio_set_function0_7 (GPIOB, MD_GPIO_PIN_3, MD_GPIO_AF0);
  355. md_gpio_set_function0_7 (GPIOB, MD_GPIO_PIN_4, MD_GPIO_AF0);
  356. md_gpio_set_function0_7 (GPIOB, MD_GPIO_PIN_5, MD_GPIO_AF0);
  357. /* Remember SPI bus1 */
  358. spi_bus = &_spi_bus1;
  359. }
  360. else
  361. {
  362. return -1;
  363. }
  364. spi_bus->parent.user_data = SPIx;
  365. return rt_spi_bus_register(spi_bus, name, &es32f0_spi_ops);
  366. }
  367. int rt_hw_spi_init(void)
  368. {
  369. int result = 0;
  370. #ifdef BSP_USING_SPI2
  371. result = es32f0_spi_register_bus(SPI2, "spi2");
  372. #endif
  373. #ifdef BSP_USING_SPI1
  374. result = es32f0_spi_register_bus(SPI1, "spi1");
  375. #endif
  376. return result;
  377. }
  378. INIT_BOARD_EXPORT(rt_hw_spi_init);
  379. #endif