drv_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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-10-19 Nations first version
  9. */
  10. #include "drv_spi.h"
  11. #if defined(RT_USING_SPI) && defined(RT_USING_PIN)
  12. #include <rtdevice.h>
  13. #if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || \
  14. defined(BSP_USING_SPI3)
  15. /* #define DEBUG */
  16. #ifdef DEBUG
  17. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  18. #else
  19. #define DEBUG_PRINTF(...)
  20. #endif
  21. /* private rt-thread spi ops function */
  22. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)
  23. {
  24. SPI_InitType SPI_InitStructure;
  25. RCC_ClocksType RCC_ClockFreq;
  26. SPI_Module* spi_periph;
  27. RT_ASSERT(device != RT_NULL);
  28. RT_ASSERT(configuration != RT_NULL);
  29. RCC_GetClocksFreqValue(&RCC_ClockFreq);
  30. spi_periph = (SPI_Module*)device->bus->parent.user_data;
  31. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  32. if (spi_periph != SPI1 && spi_periph != SPI2 && spi_periph != SPI3)
  33. {
  34. return -RT_EIO;
  35. }
  36. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  37. if (spi_periph != SPI1 && spi_periph != SPI2)
  38. {
  39. return -RT_EIO;
  40. }
  41. #endif
  42. if (configuration->data_width <= 8)
  43. {
  44. SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
  45. }
  46. else if (configuration->data_width <= 16)
  47. {
  48. SPI_InitStructure.DataLen = SPI_DATA_SIZE_16BITS;
  49. }
  50. else
  51. {
  52. return -RT_EIO;
  53. }
  54. {
  55. rt_uint32_t spi_apb_clock;
  56. rt_uint32_t max_hz;
  57. max_hz = configuration->max_hz;
  58. DEBUG_PRINTF("sys freq: %d\n", RCC_ClockFreq.SysclkFreq);
  59. DEBUG_PRINTF("CK_APB2 freq: %d\n", RCC_ClockFreq.Pclk2Freq);
  60. DEBUG_PRINTF("max freq: %d\n", max_hz);
  61. if (spi_periph == SPI1)
  62. {
  63. spi_apb_clock = RCC_ClockFreq.Pclk2Freq;
  64. }
  65. else
  66. {
  67. spi_apb_clock = RCC_ClockFreq.Pclk1Freq;
  68. }
  69. if (max_hz >= spi_apb_clock/2)
  70. {
  71. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_2;
  72. }
  73. else if (max_hz >= spi_apb_clock/4)
  74. {
  75. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_4;
  76. }
  77. else if (max_hz >= spi_apb_clock/8)
  78. {
  79. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_8;
  80. }
  81. else if (max_hz >= spi_apb_clock/16)
  82. {
  83. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_16;
  84. }
  85. else if (max_hz >= spi_apb_clock/32)
  86. {
  87. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_32;
  88. }
  89. else if (max_hz >= spi_apb_clock/64)
  90. {
  91. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_64;
  92. }
  93. else if (max_hz >= spi_apb_clock/128)
  94. {
  95. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_128;
  96. }
  97. else
  98. {
  99. /* min prescaler 256 */
  100. SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_256;
  101. }
  102. } /* baudrate */
  103. switch (configuration->mode & RT_SPI_MODE_3)
  104. {
  105. case RT_SPI_MODE_0:
  106. SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
  107. SPI_InitStructure.CLKPHA = SPI_CLKPHA_FIRST_EDGE;
  108. break;
  109. case RT_SPI_MODE_1:
  110. SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
  111. SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
  112. break;
  113. case RT_SPI_MODE_2:
  114. SPI_InitStructure.CLKPOL = SPI_CLKPOL_HIGH;
  115. SPI_InitStructure.CLKPHA = SPI_CLKPHA_FIRST_EDGE;
  116. break;
  117. case RT_SPI_MODE_3:
  118. SPI_InitStructure.CLKPOL = SPI_CLKPOL_HIGH;
  119. SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
  120. break;
  121. }
  122. /* MSB or LSB */
  123. if (configuration->mode & RT_SPI_MSB)
  124. {
  125. SPI_InitStructure.FirstBit = SPI_FB_MSB;
  126. }
  127. else
  128. {
  129. SPI_InitStructure.FirstBit = SPI_FB_LSB;
  130. }
  131. /* SPI configuration */
  132. SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
  133. SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
  134. SPI_InitStructure.NSS = SPI_NSS_SOFT;
  135. SPI_InitStructure.CRCPoly = 7;
  136. SPI_Init(spi_periph, &SPI_InitStructure);
  137. /* Enable the sFLASH_SPI */
  138. SPI_Enable(spi_periph, ENABLE);
  139. return RT_EOK;
  140. }
  141. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
  142. {
  143. struct n32_spi_cs *cs_pin = device->parent.user_data;
  144. SPI_Module* spi_periph = (SPI_Module*)device->bus->parent.user_data;
  145. struct rt_spi_configuration * config = &device->config;
  146. RT_ASSERT(device != NULL);
  147. RT_ASSERT(message != NULL);
  148. /* take CS */
  149. if (message->cs_take)
  150. {
  151. rt_pin_write(cs_pin->GPIO_Pin, PIN_LOW);
  152. DEBUG_PRINTF("spi take cs\n");
  153. }
  154. if (config->data_width <= 8)
  155. {
  156. const rt_uint8_t * send_ptr = message->send_buf;
  157. rt_uint8_t * recv_ptr = message->recv_buf;
  158. rt_uint32_t size = message->length;
  159. DEBUG_PRINTF("spi poll transfer start: %d\n", size);
  160. while (size--)
  161. {
  162. rt_uint8_t data = 0xA5;
  163. if (send_ptr != RT_NULL)
  164. {
  165. data = *send_ptr++;
  166. }
  167. /* Loop while DAT register in not emplty */
  168. while (SPI_I2S_GetStatus(spi_periph, SPI_I2S_TE_FLAG) == RESET);
  169. /* Send the byte */
  170. SPI_I2S_TransmitData(spi_periph, data);
  171. /* Wait until a data is received */
  172. while (SPI_I2S_GetStatus(spi_periph, SPI_I2S_RNE_FLAG) == RESET);
  173. /* Get the received data */
  174. data = SPI_I2S_ReceiveData(spi_periph);
  175. if (recv_ptr != RT_NULL)
  176. {
  177. *recv_ptr++ = data;
  178. }
  179. }
  180. DEBUG_PRINTF("spi poll transfer finsh\n");
  181. }
  182. else if (config->data_width <= 16)
  183. {
  184. const rt_uint16_t * send_ptr = message->send_buf;
  185. rt_uint16_t * recv_ptr = message->recv_buf;
  186. rt_uint32_t size = message->length;
  187. while (size--)
  188. {
  189. rt_uint16_t data = 0xFF;
  190. if (send_ptr != RT_NULL)
  191. {
  192. data = *send_ptr++;
  193. }
  194. /* Loop while DAT register in not emplty */
  195. while (SPI_I2S_GetStatus(spi_periph, SPI_I2S_TE_FLAG) == RESET);
  196. /* Send the byte */
  197. SPI_I2S_TransmitData(spi_periph, data);
  198. /* Wait until a data is received */
  199. while (RESET == SPI_I2S_GetStatus(spi_periph, SPI_I2S_RNE_FLAG));
  200. /* Get the received data */
  201. data = SPI_I2S_ReceiveData(spi_periph);
  202. if (recv_ptr != RT_NULL)
  203. {
  204. *recv_ptr++ = data;
  205. }
  206. }
  207. }
  208. /* release CS */
  209. if (message->cs_release)
  210. {
  211. rt_pin_write(cs_pin->GPIO_Pin, PIN_HIGH);
  212. DEBUG_PRINTF("spi release cs\n");
  213. }
  214. return message->length;
  215. }
  216. static struct rt_spi_ops spi_ops =
  217. {
  218. configure,
  219. xfer
  220. };
  221. int rt_hw_spi_init(void)
  222. {
  223. int result = 0;
  224. GPIO_InitType GPIO_InitStructure;
  225. #ifdef BSP_USING_SPI1
  226. static struct rt_spi_bus spi_bus1;
  227. spi_bus1.parent.user_data = (void *)SPI1;
  228. result = rt_spi_bus_register(&spi_bus1, "spi1", &spi_ops);
  229. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  230. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_SPI1, ENABLE);
  231. GPIO_InitStruct(&GPIO_InitStructure);
  232. /* Confige SPI1_SCLK(PA5) and SPI1_MOSI(PA7) */
  233. GPIO_InitStructure.Pin = GPIO_PIN_5 | GPIO_PIN_7;
  234. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  235. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  236. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  237. /* Confige SPI1_MISO(PA6) */
  238. GPIO_InitStructure.Pin = GPIO_PIN_6;
  239. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  240. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  241. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  242. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  243. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_SPI1 | RCC_APB2_PERIPH_AFIO, ENABLE);
  244. GPIO_InitStruct(&GPIO_InitStructure);
  245. /* Confige SPI1_SCLK(PA5) and SPI1_MISO(PA6) and SPI1_MOSI(PA7) */
  246. GPIO_InitStructure.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  247. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  248. GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI1;
  249. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  250. #endif
  251. #endif
  252. #ifdef BSP_USING_SPI2
  253. static struct rt_spi_bus spi_bus2;
  254. spi_bus2.parent.user_data = (void *)SPI2;
  255. result = rt_spi_bus_register(&spi_bus2, "spi2", &spi_ops);
  256. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  257. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  258. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_SPI2, ENABLE);
  259. GPIO_InitStruct(&GPIO_InitStructure);
  260. /* Confige SPI2_SCLK(PB13) and SPI2_MOSI(PB15) */
  261. GPIO_InitStructure.Pin = GPIO_PIN_13 | GPIO_PIN_15;
  262. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  263. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  264. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  265. /* Confige SPI2_MISO(PB14) */
  266. GPIO_InitStructure.Pin = GPIO_PIN_14;
  267. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  268. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  269. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  270. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  271. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_SPI2 | RCC_APB2_PERIPH_AFIO, ENABLE);
  272. GPIO_InitStruct(&GPIO_InitStructure);
  273. /* Confige SPI2_SCLK(PB13) and SPI2_MISO(PB14) and SPI2_MOSI(PB15) */
  274. GPIO_InitStructure.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  275. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  276. GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI2;
  277. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  278. #endif
  279. #endif
  280. #ifdef BSP_USING_SPI3
  281. static struct rt_spi_bus spi_bus3;
  282. spi_bus3.parent.user_data = (void *)SPI3;
  283. result = rt_spi_bus_register(&spi_bus3, "spi3", &spi_ops);
  284. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
  285. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_SPI3, ENABLE);
  286. GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_SW_ENABLE, ENABLE);
  287. GPIO_InitStruct(&GPIO_InitStructure);
  288. /* Confige SPI3_SCLK(PB3) and SPI3_MOSI(PB5) */
  289. GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_5;
  290. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  291. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  292. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  293. /* Confige SPI3_MISO(PB4) */
  294. GPIO_InitStructure.Pin = GPIO_PIN_4;
  295. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  296. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  297. #endif
  298. return result;
  299. }
  300. INIT_BOARD_EXPORT(rt_hw_spi_init);
  301. #endif /* defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) */
  302. #endif