drv_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. * 2022-03-04 stevetong459 first version
  9. * 2022-07-15 Aligagago add apm32F4 serie MCU support
  10. */
  11. #include "drv_spi.h"
  12. #define LOG_TAG "drv.spi"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3)
  16. static rt_err_t _spi_configure(struct rt_spi_device *spi_drv, struct rt_spi_configuration *cfg)
  17. {
  18. RT_ASSERT(spi_drv != RT_NULL);
  19. RT_ASSERT(cfg != RT_NULL);
  20. SPI_Config_T hw_spi_config;
  21. SPI_T *spi = (SPI_T *)spi_drv->bus->parent.user_data;
  22. uint32_t hw_spi_apb_clock;
  23. #if (DBG_LVL == DBG_LOG)
  24. uint32_t hw_spi_sys_clock = RCM_ReadSYSCLKFreq();
  25. #endif
  26. hw_spi_config.mode = (cfg->mode & RT_SPI_SLAVE) ? SPI_MODE_SLAVE : SPI_MODE_MASTER;
  27. hw_spi_config.direction = (cfg->mode & RT_SPI_3WIRE) ? SPI_DIRECTION_1LINE_RX : SPI_DIRECTION_2LINES_FULLDUPLEX;
  28. hw_spi_config.phase = (cfg->mode & RT_SPI_CPHA) ? SPI_CLKPHA_2EDGE : SPI_CLKPHA_1EDGE;
  29. hw_spi_config.polarity = (cfg->mode & RT_SPI_CPOL) ? SPI_CLKPOL_HIGH : SPI_CLKPOL_LOW;
  30. hw_spi_config.nss = (cfg->mode & RT_SPI_NO_CS) ? SPI_NSS_HARD : SPI_NSS_SOFT;
  31. hw_spi_config.firstBit = (cfg->mode & RT_SPI_MSB) ? SPI_FIRSTBIT_MSB : SPI_FIRSTBIT_LSB;
  32. if (cfg->data_width == 8)
  33. {
  34. hw_spi_config.length = SPI_DATA_LENGTH_8B;
  35. }
  36. else if (cfg->data_width == 16)
  37. {
  38. hw_spi_config.length = SPI_DATA_LENGTH_16B;
  39. }
  40. else
  41. {
  42. return RT_EIO;
  43. }
  44. RCM_ReadPCLKFreq(NULL, &hw_spi_apb_clock);
  45. if (cfg->max_hz >= hw_spi_apb_clock / 2)
  46. {
  47. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_2;
  48. }
  49. else if (cfg->max_hz >= hw_spi_apb_clock / 4)
  50. {
  51. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_4;
  52. }
  53. else if (cfg->max_hz >= hw_spi_apb_clock / 8)
  54. {
  55. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_8;
  56. }
  57. else if (cfg->max_hz >= hw_spi_apb_clock / 16)
  58. {
  59. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_16;
  60. }
  61. else if (cfg->max_hz >= hw_spi_apb_clock / 32)
  62. {
  63. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_32;
  64. }
  65. else if (cfg->max_hz >= hw_spi_apb_clock / 64)
  66. {
  67. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_64;
  68. }
  69. else if (cfg->max_hz >= hw_spi_apb_clock / 128)
  70. {
  71. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_128;
  72. }
  73. else
  74. {
  75. /* min prescaler 256 */
  76. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_256;
  77. }
  78. LOG_D("sys freq: %d, pclk2 freq: %d, SPI limiting freq: %d, BaudRatePrescaler: %d",
  79. hw_spi_sys_clock, hw_spi_apb_clock, cfg->max_hz, hw_spi_config.baudrateDiv);
  80. SPI_Config(spi, &hw_spi_config);
  81. SPI_Enable(spi);
  82. return RT_EOK;
  83. }
  84. static rt_uint32_t _spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  85. {
  86. RT_ASSERT(device != NULL);
  87. RT_ASSERT(message != NULL);
  88. rt_base_t cs_pin = (rt_base_t)device->parent.user_data;
  89. SPI_T *spi = (SPI_T *)device->bus->parent.user_data;
  90. struct rt_spi_configuration *config = &device->config;
  91. /* take CS */
  92. if (message->cs_take)
  93. {
  94. rt_pin_write(cs_pin, PIN_LOW);
  95. LOG_D("spi take cs\n");
  96. }
  97. if (config->data_width <= 8)
  98. {
  99. const rt_uint8_t *send_ptr = message->send_buf;
  100. rt_uint8_t *recv_ptr = message->recv_buf;
  101. rt_uint32_t size = message->length;
  102. LOG_D("spi poll transfer start: %d\n", size);
  103. while (size--)
  104. {
  105. rt_uint8_t data = 0xFF;
  106. if (send_ptr != RT_NULL)
  107. {
  108. data = *send_ptr++;
  109. }
  110. /* Wait until the transmit buffer is empty */
  111. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  112. SPI_I2S_TxData(spi, data);
  113. /* Wait until a data is received */
  114. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  115. data = SPI_I2S_RxData(spi);
  116. if (recv_ptr != RT_NULL)
  117. {
  118. *recv_ptr++ = data;
  119. }
  120. }
  121. LOG_D("spi poll transfer finsh\n");
  122. }
  123. else if (config->data_width <= 16)
  124. {
  125. const rt_uint16_t *send_ptr = message->send_buf;
  126. rt_uint16_t *recv_ptr = message->recv_buf;
  127. rt_uint32_t size = message->length;
  128. while (size--)
  129. {
  130. rt_uint16_t data = 0xFF;
  131. if (send_ptr != RT_NULL)
  132. {
  133. data = *send_ptr++;
  134. }
  135. /*Wait until the transmit buffer is empty */
  136. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  137. /* Send the byte */
  138. SPI_I2S_TxData(spi, data);
  139. /*Wait until a data is received */
  140. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  141. /* Get the received data */
  142. data = SPI_I2S_RxData(spi);
  143. if (recv_ptr != RT_NULL)
  144. {
  145. *recv_ptr++ = data;
  146. }
  147. }
  148. }
  149. /* release CS */
  150. if (message->cs_release)
  151. {
  152. rt_pin_write(cs_pin, PIN_HIGH);
  153. LOG_D("spi release cs\n");
  154. }
  155. return message->length;
  156. };
  157. static const struct rt_spi_ops _spi_ops =
  158. {
  159. _spi_configure,
  160. _spi_xfer
  161. };
  162. static int rt_hw_spi_init(void)
  163. {
  164. int result = 0;
  165. GPIO_Config_T gpio_config;
  166. #ifdef APM32F10X_HD
  167. #ifdef BSP_USING_SPI1
  168. static struct rt_spi_bus spi_bus1;
  169. spi_bus1.parent.user_data = (void *)SPI1;
  170. result = rt_spi_bus_register(&spi_bus1, "spi1", &_spi_ops);
  171. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  172. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SPI1);
  173. /* SPI1_SCK(PA5) SPI1_MOSI(PA7) */
  174. gpio_config.mode = GPIO_MODE_AF_PP;
  175. gpio_config.speed = GPIO_SPEED_50MHz;
  176. gpio_config.pin = (GPIO_PIN_5 | GPIO_PIN_7);
  177. GPIO_Config(GPIOA, &gpio_config);
  178. /* SPI1_MISO(PA6) */
  179. gpio_config.mode = GPIO_MODE_IN_FLOATING;
  180. gpio_config.speed = GPIO_SPEED_50MHz;
  181. gpio_config.pin = GPIO_PIN_6;
  182. GPIO_Config(GPIOA, &gpio_config);
  183. #endif
  184. #ifdef BSP_USING_SPI2
  185. static struct rt_spi_bus spi_bus2;
  186. spi_bus2.parent.user_data = (void *)SPI2;
  187. result = rt_spi_bus_register(&spi_bus2, "spi2", &_spi_ops);
  188. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  189. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_SPI2);
  190. /* SPI2_SCK(PB13) SPI2_MOSI(PB15) */
  191. gpio_config.mode = GPIO_MODE_AF_PP;
  192. gpio_config.speed = GPIO_SPEED_50MHz;
  193. gpio_config.pin = (GPIO_PIN_13 | GPIO_PIN_15);
  194. GPIO_Config(GPIOB, &gpio_config);
  195. /* SPI2_MISO(PB14) */
  196. gpio_config.mode = GPIO_MODE_IN_FLOATING;
  197. gpio_config.speed = GPIO_SPEED_50MHz;
  198. gpio_config.pin = GPIO_PIN_14;
  199. GPIO_Config(GPIOB, &gpio_config);
  200. #endif
  201. #ifdef BSP_USING_SPI3
  202. static struct rt_spi_bus spi_bus3;
  203. spi_bus3.parent.user_data = (void *)SPI3;
  204. result = rt_spi_bus_register(&spi_bus3, "spi3", &_spi_ops);
  205. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  206. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_SPI3);
  207. /* SPI3_SCK(PB3) SPI3_MOSI(PB5) */
  208. gpio_config.mode = GPIO_MODE_AF_PP;
  209. gpio_config.speed = GPIO_SPEED_50MHz;
  210. gpio_config.pin = (GPIO_PIN_3 | GPIO_PIN_5);
  211. GPIO_Config(GPIOB, &gpio_config);
  212. /* SPI3_MISO(PB4) */
  213. gpio_config.mode = GPIO_MODE_IN_FLOATING;
  214. gpio_config.speed = GPIO_SPEED_50MHz;
  215. gpio_config.pin = GPIO_PIN_4;
  216. GPIO_Config(GPIOB, &gpio_config);
  217. #endif
  218. #elif APM32F40X
  219. #ifdef BSP_USING_SPI1
  220. static struct rt_spi_bus spi_bus1;
  221. spi_bus1.parent.user_data = (void *)SPI1;
  222. result = rt_spi_bus_register(&spi_bus1, "spi1", &_spi_ops);
  223. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
  224. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SPI1);
  225. /* Config SPI1 PinAF */
  226. GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_4, GPIO_AF_SPI1);
  227. GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_5, GPIO_AF_SPI1);
  228. GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_6, GPIO_AF_SPI1);
  229. GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_7, GPIO_AF_SPI1);
  230. /* SPI1_NSS(PA4) SPI1_SCK(PA5) SPI1_MISO(PA6) SPI1_MOSI(PA7) */
  231. gpio_config.mode = GPIO_MODE_AF;
  232. gpio_config.speed = GPIO_SPEED_100MHz;
  233. gpio_config.otype = GPIO_OTYPE_PP;
  234. gpio_config.pupd = GPIO_PUPD_NOPULL;
  235. gpio_config.pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  236. GPIO_Config(GPIOA, &gpio_config);
  237. #endif
  238. #ifdef BSP_USING_SPI2
  239. static struct rt_spi_bus spi_bus2;
  240. spi_bus2.parent.user_data = (void *)SPI2;
  241. result = rt_spi_bus_register(&spi_bus2, "spi2", &_spi_ops);
  242. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOB);
  243. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_SPI2);
  244. /* Config SPI2 PinAF */
  245. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_12, GPIO_AF_SPI2);
  246. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_13, GPIO_AF_SPI2);
  247. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_14, GPIO_AF_SPI2);
  248. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_15, GPIO_AF_SPI2);
  249. /* SPI2_NSS(PB12) SPI2_SCK(PB13) SPI2_MISO(PB14) SPI2_MOSI(PB15) */
  250. gpio_config.mode = GPIO_MODE_AF;
  251. gpio_config.speed = GPIO_SPEED_100MHz;
  252. gpio_config.otype = GPIO_OTYPE_PP;
  253. gpio_config.pupd = GPIO_PUPD_NOPULL;
  254. gpio_config.pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  255. GPIO_Config(GPIOB, &gpio_config);
  256. #endif
  257. #ifdef BSP_USING_SPI3
  258. static struct rt_spi_bus spi_bus3;
  259. spi_bus3.parent.user_data = (void *)SPI3;
  260. result = rt_spi_bus_register(&spi_bus3, "spi3", &_spi_ops);
  261. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
  262. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOB);
  263. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_SPI3);
  264. /* Config SPI3 PinAF */
  265. GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_4, GPIO_AF_SPI3);
  266. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_3, GPIO_AF_SPI3);
  267. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_4, GPIO_AF_SPI3);
  268. GPIO_ConfigPinAF(GPIOB, GPIO_PIN_SOURCE_5, GPIO_AF_SPI3);
  269. /* SPI3_SCK(PB3) SPI3_MISO(PB4) SPI3_MOSI(PB5) */
  270. gpio_config.mode = GPIO_MODE_AF;
  271. gpio_config.speed = GPIO_SPEED_100MHz;
  272. gpio_config.otype = GPIO_OTYPE_PP;
  273. gpio_config.pupd = GPIO_PUPD_NOPULL;
  274. gpio_config.pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
  275. GPIO_Config(GPIOB, &gpio_config);
  276. /* SPI3_NSS(PA4) */
  277. gpio_config.pin = GPIO_PIN_4;
  278. GPIO_Config(GPIOA, &gpio_config);
  279. #endif
  280. #endif
  281. return result;
  282. }
  283. INIT_BOARD_EXPORT(rt_hw_spi_init);
  284. #endif /* RT_USING_SPI */