drv_spi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-15 Jonas first version
  9. */
  10. #include <board.h>
  11. #include "drv_spi.h"
  12. #ifdef RT_USING_SPI
  13. #if !defined(BSP_USING_SPI1) && !defined(BSP_USING_SPI2) && \
  14. !defined(BSP_USING_SPI3) && !defined(BSP_USING_SPI4)
  15. #error "Please define at least one SPIx"
  16. #endif
  17. //#define DEBUG
  18. #define ARR_LEN(__N) (sizeof(__N) / sizeof(__N[0]))
  19. #ifdef DEBUG
  20. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  21. #else
  22. #define DEBUG_PRINTF(...)
  23. #endif
  24. /* private rt-thread spi ops function */
  25. static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  26. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  27. static struct rt_spi_ops hk32_spi_ops =
  28. {
  29. configure,
  30. xfer
  31. };
  32. /**
  33. * Attach the spi device to SPI bus, this function must be used after initialization.
  34. */
  35. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin)
  36. {
  37. RT_ASSERT(bus_name != RT_NULL);
  38. RT_ASSERT(device_name != RT_NULL);
  39. rt_err_t result;
  40. struct rt_spi_device *spi_device;
  41. struct hk32_spi_cs *cs_pin;
  42. /* initialize the cs pin && select the slave*/
  43. GPIO_InitTypeDef GPIO_InitStruct;
  44. GPIO_InitStruct.GPIO_Pin = cs_gpio_pin;
  45. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  46. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  47. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  48. GPIO_Init(cs_gpiox, &GPIO_InitStruct);
  49. GPIO_SetBits(cs_gpiox, cs_gpio_pin);
  50. /* attach the device to spi bus*/
  51. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  52. RT_ASSERT(spi_device != RT_NULL);
  53. cs_pin = (struct hk32_spi_cs *)rt_malloc(sizeof(struct hk32_spi_cs));
  54. RT_ASSERT(cs_pin != RT_NULL);
  55. cs_pin->GPIOx = cs_gpiox;
  56. cs_pin->GPIO_Pin = cs_gpio_pin;
  57. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  58. if (result != RT_EOK)
  59. {
  60. DEBUG_PRINTF("%s attach to %s faild, %d\n", device_name, bus_name, result);
  61. }
  62. RT_ASSERT(result == RT_EOK);
  63. DEBUG_PRINTF("%s attach to %s done", device_name, bus_name);
  64. return result;
  65. }
  66. static rt_err_t configure(struct rt_spi_device *device,
  67. struct rt_spi_configuration *configuration)
  68. {
  69. struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
  70. struct hk32_spi *spi_instance = (struct hk32_spi *)spi_bus->parent.user_data;
  71. SPI_InitTypeDef SPI_InitStruct;
  72. RT_ASSERT(device != RT_NULL);
  73. RT_ASSERT(configuration != RT_NULL);
  74. hk32_msp_spi_init(spi_instance->config->spix);
  75. /* data_width */
  76. if (configuration->data_width <= 8)
  77. {
  78. SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
  79. }
  80. else if (configuration->data_width <= 16)
  81. {
  82. SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b;
  83. }
  84. else
  85. {
  86. return -RT_EIO;
  87. }
  88. /* baudrate */
  89. {
  90. uint32_t spi_apb_clock;
  91. uint32_t max_hz;
  92. RCC_ClocksTypeDef RCC_Clocks;
  93. max_hz = configuration->max_hz;
  94. RCC_GetClocksFreq(&RCC_Clocks);
  95. DEBUG_PRINTF("sys freq: %d\n", RCC_Clocks.SYSCLK_Freq);
  96. DEBUG_PRINTF("max freq: %d\n", max_hz);
  97. if (spi_instance->config->spix == SPI1)
  98. {
  99. spi_apb_clock = RCC_Clocks.PCLK_Frequency;
  100. DEBUG_PRINTF("pclk freq: %d\n", RCC_Clocks.PCLK_Frequency);
  101. }
  102. else
  103. {
  104. spi_apb_clock = RCC_Clocks.PCLK_Frequency;
  105. DEBUG_PRINTF("pclk1 freq: %d\n", RCC_Clocks.PCLK_Frequency);
  106. }
  107. if (max_hz >= spi_apb_clock / 2)
  108. {
  109. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  110. }
  111. else if (max_hz >= spi_apb_clock / 4)
  112. {
  113. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  114. }
  115. else if (max_hz >= spi_apb_clock / 8)
  116. {
  117. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
  118. }
  119. else if (max_hz >= spi_apb_clock / 16)
  120. {
  121. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  122. }
  123. else if (max_hz >= spi_apb_clock / 32)
  124. {
  125. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
  126. }
  127. else if (max_hz >= spi_apb_clock / 64)
  128. {
  129. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
  130. }
  131. else if (max_hz >= spi_apb_clock / 128)
  132. {
  133. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
  134. }
  135. else
  136. {
  137. /* min prescaler 256 */
  138. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  139. }
  140. } /* baudrate */
  141. switch (configuration->mode & RT_SPI_MODE_3)
  142. {
  143. case RT_SPI_MODE_0:
  144. SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
  145. SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
  146. break;
  147. case RT_SPI_MODE_1:
  148. SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
  149. SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
  150. break;
  151. case RT_SPI_MODE_2:
  152. SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
  153. SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
  154. break;
  155. case RT_SPI_MODE_3:
  156. SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
  157. SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
  158. break;
  159. }
  160. /* MSB or LSB */
  161. if (configuration->mode & RT_SPI_MSB)
  162. {
  163. SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
  164. }
  165. else
  166. {
  167. SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_LSB;
  168. }
  169. SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  170. SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
  171. SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
  172. /* init SPI */
  173. SPI_Init(spi_instance->config->spix, &SPI_InitStruct);
  174. /* Enable SPI_MASTER */
  175. SPI_Cmd(spi_instance->config->spix, ENABLE);
  176. SPI_CalculateCRC(spi_instance->config->spix, DISABLE);
  177. return RT_EOK;
  178. };
  179. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  180. {
  181. struct rt_spi_bus *hk32_spi_bus = (struct rt_spi_bus *)device->bus;
  182. struct hk32_spi *spi_instance = (struct hk32_spi *)hk32_spi_bus->parent.user_data;
  183. struct rt_spi_configuration *config = &device->config;
  184. struct hk32_spi_cs *hk32_spi_cs = device->parent.user_data;
  185. RT_ASSERT(device != NULL);
  186. RT_ASSERT(message != NULL);
  187. /* take CS */
  188. if (message->cs_take)
  189. {
  190. GPIO_ResetBits(hk32_spi_cs->GPIOx, hk32_spi_cs->GPIO_Pin);
  191. DEBUG_PRINTF("spi take cs\n");
  192. }
  193. {
  194. if (config->data_width <= 8)
  195. {
  196. const rt_uint8_t *send_ptr = message->send_buf;
  197. rt_uint8_t *recv_ptr = message->recv_buf;
  198. rt_uint32_t size = message->length;
  199. DEBUG_PRINTF("spi poll transfer start: %d\n", size);
  200. while (size--)
  201. {
  202. rt_uint8_t data = 0xFF;
  203. if (send_ptr != RT_NULL)
  204. {
  205. data = *send_ptr++;
  206. }
  207. /* Todo: replace register read/write by hk32 lib */
  208. /* Wait until the transmit buffer is empty */
  209. while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_TXE));
  210. /* Send the byte */
  211. SPI_SendData8(spi_instance->config->spix, data);
  212. /* Wait until a data is received */
  213. while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_RXNE));
  214. /* Get the received data */
  215. data = SPI_ReceiveData8(spi_instance->config->spix);
  216. if (recv_ptr != RT_NULL)
  217. {
  218. *recv_ptr++ = data;
  219. }
  220. }
  221. DEBUG_PRINTF("spi poll transfer finsh\n");
  222. }
  223. else if (config->data_width <= 16)
  224. {
  225. const rt_uint16_t *send_ptr = message->send_buf;
  226. rt_uint16_t *recv_ptr = message->recv_buf;
  227. rt_uint32_t size = message->length;
  228. while (size--)
  229. {
  230. rt_uint16_t data = 0xFF;
  231. if (send_ptr != RT_NULL)
  232. {
  233. data = *send_ptr++;
  234. }
  235. /* Wait until the transmit buffer is empty */
  236. while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_TXE));
  237. /* Send the byte */
  238. SPI_I2S_SendData16(spi_instance->config->spix, data);
  239. /* Wait until a data is received */
  240. while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_RXNE));
  241. /* Get the received data */
  242. data = SPI_I2S_ReceiveData16(spi_instance->config->spix);
  243. if (recv_ptr != RT_NULL)
  244. {
  245. *recv_ptr++ = data;
  246. }
  247. }
  248. }
  249. }
  250. /* release CS */
  251. if (message->cs_release)
  252. {
  253. GPIO_SetBits(hk32_spi_cs->GPIOx, hk32_spi_cs->GPIO_Pin);
  254. DEBUG_PRINTF("spi release cs\n");
  255. }
  256. return message->length;
  257. };
  258. static struct hk32_spi_config configs[] =
  259. {
  260. #ifdef BSP_USING_SPI1
  261. {SPI1, "spi1"},
  262. #endif
  263. #ifdef BSP_USING_SPI2
  264. {SPI2, "spi2"},
  265. #endif
  266. #ifdef BSP_USING_SPI3
  267. {SPI3, "spi3"},
  268. #endif
  269. #ifdef BSP_USING_SPI4
  270. {SPI4, "spi4"},
  271. #endif
  272. };
  273. static struct hk32_spi spis[sizeof(configs) / sizeof(configs[0])] = {0};
  274. int rt_hw_spi_init(void)
  275. {
  276. int i;
  277. rt_err_t result;
  278. rt_size_t obj_num = sizeof(spis) / sizeof(struct hk32_spi);
  279. for (i = 0; i < obj_num; i++)
  280. {
  281. spis[i].config = &configs[i];
  282. spis[i].spi_bus.parent.user_data = (void *)&spis[i];
  283. result = rt_spi_bus_register(&(spis[i].spi_bus), spis[i].config->spi_name, &hk32_spi_ops);
  284. }
  285. return result;
  286. }
  287. INIT_BOARD_EXPORT(rt_hw_spi_init);
  288. #endif