drv_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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-03-04 stevetong459 first version
  9. * 2022-07-15 Aligagago add APM32F4 series MCU support
  10. * 2022-12-26 luobeihai add APM32F0 series MCU support
  11. * 2023-03-28 luobeihai add APM32E1/S1 series MCU support
  12. */
  13. #include "drv_spi.h"
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.spi"
  16. #include "drv_log.h"
  17. #if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3)
  18. static struct apm32_spi_config spi_config[] = {
  19. #ifdef BSP_USING_SPI1
  20. {SPI1, "spi1"},
  21. #endif
  22. #ifdef BSP_USING_SPI2
  23. {SPI2, "spi2"},
  24. #endif
  25. #ifdef BSP_USING_SPI3
  26. {SPI3, "spi3"},
  27. #endif
  28. };
  29. static struct apm32_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])] = {0};
  30. /**
  31. * Attach the spi device to SPI bus, this function must be used after initialization.
  32. */
  33. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_T *cs_gpiox, uint16_t cs_gpio_pin)
  34. {
  35. RT_ASSERT(bus_name != RT_NULL);
  36. RT_ASSERT(device_name != RT_NULL);
  37. rt_err_t result;
  38. struct rt_spi_device *spi_device;
  39. struct apm32_spi_cs *cs_pin;
  40. GPIO_Config_T GPIO_InitStructure;
  41. /* initialize the cs pin && select the slave */
  42. #if defined(SOC_SERIES_APM32F0)
  43. GPIO_ConfigStructInit(&GPIO_InitStructure);
  44. GPIO_InitStructure.pin = cs_gpio_pin;
  45. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  46. GPIO_InitStructure.mode = GPIO_MODE_OUT;
  47. GPIO_InitStructure.outtype = GPIO_OUT_TYPE_PP;
  48. GPIO_InitStructure.pupd = GPIO_PUPD_NO;
  49. GPIO_Config(cs_gpiox, &GPIO_InitStructure);
  50. GPIO_WriteBitValue(cs_gpiox, cs_gpio_pin, Bit_SET);
  51. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  52. GPIO_ConfigStructInit(&GPIO_InitStructure);
  53. GPIO_InitStructure.pin = cs_gpio_pin;
  54. GPIO_InitStructure.mode = GPIO_MODE_OUT_PP;
  55. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  56. GPIO_Config(cs_gpiox, &GPIO_InitStructure);
  57. GPIO_WriteBitValue(cs_gpiox, cs_gpio_pin, BIT_SET);
  58. #elif defined(SOC_SERIES_APM32F4)
  59. GPIO_ConfigStructInit(&GPIO_InitStructure);
  60. GPIO_InitStructure.pin = cs_gpio_pin;
  61. GPIO_InitStructure.speed = GPIO_SPEED_100MHz;
  62. GPIO_InitStructure.mode = GPIO_MODE_OUT;
  63. GPIO_InitStructure.otype = GPIO_OTYPE_PP;
  64. GPIO_InitStructure.pupd = GPIO_PUPD_NOPULL;
  65. GPIO_Config(cs_gpiox, &GPIO_InitStructure);
  66. GPIO_WriteBitValue(cs_gpiox, cs_gpio_pin, BIT_SET);
  67. #endif
  68. /* attach the device to spi bus */
  69. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  70. RT_ASSERT(spi_device != RT_NULL);
  71. cs_pin = (struct apm32_spi_cs *)rt_malloc(sizeof(struct apm32_spi_cs));
  72. RT_ASSERT(cs_pin != RT_NULL);
  73. cs_pin->GPIOx = cs_gpiox;
  74. cs_pin->GPIO_Pin = cs_gpio_pin;
  75. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  76. if (result != RT_EOK)
  77. {
  78. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  79. }
  80. RT_ASSERT(result == RT_EOK);
  81. LOG_D("%s attach to %s done", device_name, bus_name);
  82. return result;
  83. }
  84. static rt_err_t apm32_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  85. {
  86. RT_ASSERT(device != RT_NULL);
  87. RT_ASSERT(cfg != RT_NULL);
  88. SPI_Config_T hw_spi_config;
  89. struct rt_spi_bus * apm32_spi_bus = (struct rt_spi_bus *)device->bus;
  90. struct apm32_spi *spi_device = (struct apm32_spi *)apm32_spi_bus->parent.user_data;
  91. SPI_T *spi = spi_device->config->spi_x;
  92. uint32_t hw_spi_apb_clock;
  93. #if (DBG_LVL == DBG_LOG)
  94. uint32_t hw_spi_sys_clock = RCM_ReadSYSCLKFreq();
  95. #endif
  96. /* apm32 spi gpio init and enable clock */
  97. extern void apm32_msp_spi_init(void *Instance);
  98. apm32_msp_spi_init(spi);
  99. /* apm32 spi init */
  100. hw_spi_config.mode = (cfg->mode & RT_SPI_SLAVE) ? SPI_MODE_SLAVE : SPI_MODE_MASTER;
  101. hw_spi_config.direction = (cfg->mode & RT_SPI_3WIRE) ? SPI_DIRECTION_1LINE_RX : SPI_DIRECTION_2LINES_FULLDUPLEX;
  102. hw_spi_config.phase = (cfg->mode & RT_SPI_CPHA) ? SPI_CLKPHA_2EDGE : SPI_CLKPHA_1EDGE;
  103. hw_spi_config.polarity = (cfg->mode & RT_SPI_CPOL) ? SPI_CLKPOL_HIGH : SPI_CLKPOL_LOW;
  104. #if defined(SOC_SERIES_APM32F0)
  105. hw_spi_config.slaveSelect = (cfg->mode & RT_SPI_NO_CS) ? SPI_SSC_DISABLE : SPI_SSC_ENABLE;
  106. hw_spi_config.firstBit = (cfg->mode & RT_SPI_MSB) ? SPI_FIRST_BIT_MSB : SPI_FIRST_BIT_LSB;
  107. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  108. || defined(SOC_SERIES_APM32F4)
  109. hw_spi_config.nss = (cfg->mode & RT_SPI_NO_CS) ? SPI_NSS_HARD : SPI_NSS_SOFT;
  110. hw_spi_config.firstBit = (cfg->mode & RT_SPI_MSB) ? SPI_FIRSTBIT_MSB : SPI_FIRSTBIT_LSB;
  111. #endif
  112. if (cfg->data_width == 8)
  113. {
  114. hw_spi_config.length = SPI_DATA_LENGTH_8B;
  115. }
  116. else if (cfg->data_width == 16)
  117. {
  118. hw_spi_config.length = SPI_DATA_LENGTH_16B;
  119. }
  120. else
  121. {
  122. return -RT_EIO;
  123. }
  124. #if defined(SOC_SERIES_APM32F0)
  125. hw_spi_apb_clock = RCM_ReadPCLKFreq();
  126. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  127. || defined(SOC_SERIES_APM32F4)
  128. if (spi == SPI1)
  129. {
  130. RCM_ReadPCLKFreq(NULL, &hw_spi_apb_clock);
  131. }
  132. else
  133. {
  134. RCM_ReadPCLKFreq(&hw_spi_apb_clock, NULL);
  135. }
  136. #endif
  137. if (cfg->max_hz >= hw_spi_apb_clock / 2)
  138. {
  139. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_2;
  140. }
  141. else if (cfg->max_hz >= hw_spi_apb_clock / 4)
  142. {
  143. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_4;
  144. }
  145. else if (cfg->max_hz >= hw_spi_apb_clock / 8)
  146. {
  147. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_8;
  148. }
  149. else if (cfg->max_hz >= hw_spi_apb_clock / 16)
  150. {
  151. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_16;
  152. }
  153. else if (cfg->max_hz >= hw_spi_apb_clock / 32)
  154. {
  155. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_32;
  156. }
  157. else if (cfg->max_hz >= hw_spi_apb_clock / 64)
  158. {
  159. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_64;
  160. }
  161. else if (cfg->max_hz >= hw_spi_apb_clock / 128)
  162. {
  163. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_128;
  164. }
  165. else
  166. {
  167. /* min prescaler 256 */
  168. hw_spi_config.baudrateDiv = SPI_BAUDRATE_DIV_256;
  169. }
  170. LOG_D("sys freq: %d, pclk2 freq: %d, SPI limiting freq: %d, BaudRatePrescaler: %d",
  171. hw_spi_sys_clock, hw_spi_apb_clock, cfg->max_hz, hw_spi_config.baudrateDiv);
  172. #if defined(SOC_SERIES_APM32F0)
  173. SPI_DisableCRC(spi);
  174. SPI_EnableSSoutput(spi);
  175. SPI_ConfigFIFOThreshold(spi, SPI_RXFIFO_QUARTER);
  176. #endif
  177. SPI_Config(spi, &hw_spi_config);
  178. SPI_Enable(spi);
  179. return RT_EOK;
  180. }
  181. static rt_ssize_t apm32_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  182. {
  183. RT_ASSERT(device != NULL);
  184. RT_ASSERT(message != NULL);
  185. struct rt_spi_configuration *config = &device->config;
  186. struct apm32_spi_cs *cs = device->parent.user_data;
  187. struct rt_spi_bus * apm32_spi_bus = (struct rt_spi_bus *)device->bus;
  188. struct apm32_spi *spi_device = (struct apm32_spi *)apm32_spi_bus->parent.user_data;
  189. SPI_T *spi = spi_device->config->spi_x;
  190. /* take CS */
  191. if (message->cs_take)
  192. {
  193. #if defined(SOC_SERIES_APM32F0)
  194. GPIO_WriteBitValue(cs->GPIOx, cs->GPIO_Pin, (GPIO_BSRET_T)RESET);
  195. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  196. || defined(SOC_SERIES_APM32F4)
  197. GPIO_WriteBitValue(cs->GPIOx, cs->GPIO_Pin, RESET);
  198. #endif
  199. LOG_D("spi take cs\n");
  200. }
  201. if (config->data_width <= 8)
  202. {
  203. const rt_uint8_t *send_ptr = message->send_buf;
  204. rt_uint8_t *recv_ptr = message->recv_buf;
  205. rt_uint32_t size = message->length;
  206. LOG_D("spi poll transfer start: %d\n", size);
  207. while (size--)
  208. {
  209. rt_uint8_t data = 0xFF;
  210. if (send_ptr != RT_NULL)
  211. {
  212. data = *send_ptr++;
  213. }
  214. #if defined(SOC_SERIES_APM32F0)
  215. /* Wait until the transmit buffer is empty */
  216. while (SPI_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  217. SPI_TxData8(spi, data);
  218. /* Wait until a data is received */
  219. while (SPI_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  220. data = SPI_RxData8(spi);
  221. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32F4)
  222. /* Wait until the transmit buffer is empty */
  223. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  224. SPI_I2S_TxData(spi, data);
  225. /* Wait until a data is received */
  226. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  227. data = SPI_I2S_RxData(spi);
  228. #elif defined(SOC_SERIES_APM32S1)
  229. /* Wait until the transmit buffer is empty */
  230. while (SPI_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  231. SPI_TxData(spi, data);
  232. /* Wait until a data is received */
  233. while (SPI_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  234. data = SPI_RxData(spi);
  235. #endif
  236. if (recv_ptr != RT_NULL)
  237. {
  238. *recv_ptr++ = data;
  239. }
  240. }
  241. LOG_D("spi poll transfer finsh\n");
  242. }
  243. else if (config->data_width <= 16)
  244. {
  245. const rt_uint16_t *send_ptr = message->send_buf;
  246. rt_uint16_t *recv_ptr = message->recv_buf;
  247. rt_uint32_t size = message->length;
  248. while (size--)
  249. {
  250. rt_uint16_t data = 0xFF;
  251. if (send_ptr != RT_NULL)
  252. {
  253. data = *send_ptr++;
  254. }
  255. #if defined(SOC_SERIES_APM32F0)
  256. /* Wait until the transmit buffer is empty */
  257. while (SPI_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  258. SPI_I2S_TxData16(spi, data);
  259. /* Wait until a data is received */
  260. while (SPI_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  261. data = SPI_I2S_RxData16(spi);
  262. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32F4)
  263. /* Wait until the transmit buffer is empty */
  264. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  265. /* Send the byte */
  266. SPI_I2S_TxData(spi, data);
  267. /* Wait until a data is received */
  268. while (SPI_I2S_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  269. /* Get the received data */
  270. data = SPI_I2S_RxData(spi);
  271. #elif defined(SOC_SERIES_APM32S1)
  272. /* Wait until the transmit buffer is empty */
  273. while (SPI_ReadStatusFlag(spi, SPI_FLAG_TXBE) == RESET);
  274. /* Send the byte */
  275. SPI_TxData(spi, data);
  276. /* Wait until a data is received */
  277. while (SPI_ReadStatusFlag(spi, SPI_FLAG_RXBNE) == RESET);
  278. /* Get the received data */
  279. data = SPI_RxData(spi);
  280. #endif
  281. if (recv_ptr != RT_NULL)
  282. {
  283. *recv_ptr++ = data;
  284. }
  285. }
  286. }
  287. /* release CS */
  288. if (message->cs_release)
  289. {
  290. #if defined(SOC_SERIES_APM32F0)
  291. GPIO_WriteBitValue(cs->GPIOx, cs->GPIO_Pin, (GPIO_BSRET_T)SET);
  292. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  293. || defined(SOC_SERIES_APM32F4)
  294. GPIO_WriteBitValue(cs->GPIOx, cs->GPIO_Pin, SET);
  295. #endif
  296. LOG_D("spi release cs\n");
  297. }
  298. return message->length;
  299. };
  300. static const struct rt_spi_ops apm32_spi_ops =
  301. {
  302. apm32_spi_configure,
  303. apm32_spi_xfer
  304. };
  305. static int rt_hw_spi_init(void)
  306. {
  307. rt_err_t result;
  308. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  309. {
  310. spi_bus_obj[i].config = &spi_config[i];
  311. spi_bus_obj[i].spi_bus.parent.user_data = (void *)&spi_bus_obj[i];
  312. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].spi_bus_name, &apm32_spi_ops);
  313. RT_ASSERT(result == RT_EOK);
  314. LOG_D("%s bus init done", spi_config[i].spi_bus_name);
  315. }
  316. return result;
  317. }
  318. INIT_BOARD_EXPORT(rt_hw_spi_init);
  319. #endif /* RT_USING_SPI */