drv_spi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * File : drv_spi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2016-09-02 Aubr.Cool the first version
  13. */
  14. #include <stm32l0xx.h>
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include <board.h>
  19. #ifdef RT_USING_COMPONENTS_INIT
  20. #include <components.h>
  21. #endif
  22. #define SPIRXEVENT 0x01
  23. #define SPITXEVENT 0x02
  24. #ifdef RT_USING_SPI
  25. #define SPITIMEOUT 2
  26. #define SPICRCEN 0
  27. struct stm32_hw_spi;
  28. typedef void(*spiirqapi)(struct stm32_hw_spi *hspi);
  29. struct stm32_hw_spi {
  30. SPI_TypeDef* Instance;
  31. struct rt_spi_configuration* cfg;
  32. };
  33. struct stm32_spi {
  34. SPI_TypeDef* spi_device;
  35. struct stm32_hw_spi *data;
  36. };
  37. struct stm32_hw_spi_cs {
  38. rt_uint32_t pin;
  39. };
  40. static rt_err_t stml0xx_spi_init(SPI_TypeDef * spix, struct rt_spi_configuration * cfg)
  41. {
  42. SPI_HandleTypeDef hspi;
  43. hspi.Instance = spix;
  44. if(cfg->mode & RT_SPI_SLAVE) {
  45. hspi.Init.Mode = SPI_MODE_SLAVE;
  46. } else {
  47. hspi.Init.Mode = SPI_MODE_MASTER;
  48. }
  49. if(cfg->mode & RT_SPI_3WIRE) {
  50. hspi.Init.Direction = SPI_DIRECTION_1LINE;
  51. } else {
  52. hspi.Init.Direction = SPI_DIRECTION_2LINES;
  53. }
  54. if(cfg->data_width == 8) {
  55. hspi.Init.DataSize = SPI_DATASIZE_8BIT;
  56. } else if(cfg->data_width == 16) {
  57. hspi.Init.DataSize = SPI_DATASIZE_16BIT;
  58. } else {
  59. return RT_EIO;
  60. }
  61. if(cfg->mode & RT_SPI_CPHA) {
  62. hspi.Init.CLKPhase = SPI_PHASE_2EDGE;
  63. } else {
  64. hspi.Init.CLKPhase = SPI_PHASE_1EDGE;
  65. }
  66. if(cfg->mode & RT_SPI_CPOL) {
  67. hspi.Init.CLKPolarity = SPI_POLARITY_HIGH;
  68. } else {
  69. hspi.Init.CLKPolarity = SPI_POLARITY_LOW;
  70. }
  71. if(cfg->mode & RT_SPI_NO_CS) {
  72. hspi.Init.NSS = SPI_NSS_SOFT;
  73. } else {
  74. hspi.Init.NSS = SPI_NSS_HARD_OUTPUT;
  75. }
  76. hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  77. if(cfg->mode & RT_SPI_MSB) {
  78. hspi.Init.FirstBit = SPI_FIRSTBIT_MSB;
  79. } else {
  80. hspi.Init.FirstBit = SPI_FIRSTBIT_LSB;
  81. }
  82. hspi.Init.TIMode = SPI_TIMODE_DISABLE;
  83. hspi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  84. hspi.Init.CRCPolynomial = 7;
  85. if (HAL_SPI_Init(&hspi) != HAL_OK)
  86. {
  87. return RT_EIO;
  88. }
  89. __HAL_SPI_ENABLE(&hspi);
  90. return RT_EOK;
  91. }
  92. #define SPISTEP(datalen) (((datalen) == 8) ? 1 : 2)
  93. #define SPISEND_1(reg, ptr, datalen) \
  94. do {\
  95. if(datalen == 8) { \
  96. (reg) = *(rt_uint8_t *)(ptr); \
  97. } else { \
  98. (reg) = *(rt_uint16_t *) (ptr); \
  99. } \
  100. } while(0)
  101. #define SPIRECV_1(reg, ptr, datalen) \
  102. do {\
  103. if(datalen == 8) { \
  104. *(rt_uint8_t *)(ptr) = (reg); \
  105. } else { \
  106. *(rt_uint16_t *) (ptr) = reg; \
  107. } \
  108. } while(0)
  109. static rt_err_t spitxrx1b(struct stm32_hw_spi *hspi, void *rcvb, const void *sndb)
  110. {
  111. rt_uint32_t padrcv = 0;
  112. rt_uint32_t padsnd = 0xFF;
  113. if(! rcvb && !sndb) {
  114. return RT_ERROR;
  115. }
  116. if(!rcvb) {
  117. rcvb = &padrcv;
  118. }
  119. if(!sndb) {
  120. sndb = &padsnd;
  121. }
  122. while(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE) == RESET);
  123. SPISEND_1(hspi->Instance->DR, sndb, hspi->cfg->data_width);
  124. while(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE) == RESET);
  125. SPIRECV_1(hspi->Instance->DR, rcvb, hspi->cfg->data_width);
  126. return RT_EOK;
  127. }
  128. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  129. {
  130. rt_err_t res;
  131. RT_ASSERT(device != RT_NULL);
  132. RT_ASSERT(device->bus != RT_NULL);
  133. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  134. struct stm32_spi* spix;
  135. spix = (struct stm32_spi *)device->bus->parent.user_data;
  136. struct stm32_hw_spi *hspi = spix->data;
  137. struct stm32_hw_spi_cs * cs = device->parent.user_data;
  138. if(message->cs_take) {
  139. rt_pin_write(cs->pin, 0);
  140. }
  141. const rt_uint8_t *sndb = message->send_buf;
  142. rt_uint8_t *rcvb = message->recv_buf;
  143. rt_int32_t length = message->length;
  144. while(length) {
  145. res = spitxrx1b(hspi, rcvb, sndb);
  146. if(rcvb) {
  147. rcvb += SPISTEP(hspi->cfg->data_width);
  148. }
  149. if(sndb) {
  150. sndb += SPISTEP(hspi->cfg->data_width);
  151. }
  152. if(res != RT_EOK) {
  153. break;
  154. }
  155. length--;
  156. }
  157. /* Wait until Busy flag is reset before disabling SPI */
  158. while(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_BSY) == SET);
  159. if(message->cs_release) {
  160. rt_pin_write(cs->pin, 1);
  161. }
  162. return message->length - length;
  163. }
  164. #ifdef RT_USING_SPI1
  165. static struct stm32_hw_spi spi1hwdata = {
  166. .Instance = SPI1,
  167. };
  168. const static struct stm32_spi spi1 = {
  169. SPI1,
  170. &spi1hwdata,
  171. };
  172. const static struct stm32_hw_spi_cs stm32_spi1_cs = {
  173. SPI1PINNSS,
  174. };
  175. rt_err_t spi1configure(struct rt_spi_device *device,
  176. struct rt_spi_configuration *configuration)
  177. {
  178. spi1hwdata.cfg = configuration;
  179. return stml0xx_spi_init(spi1.spi_device, configuration);
  180. }
  181. const struct rt_spi_ops stm_spi_ops1 =
  182. {
  183. .configure = spi1configure,
  184. .xfer = spixfer,
  185. };
  186. static struct rt_spi_bus stm_spi_bus1 = {
  187. .parent = {
  188. .user_data = (void *)&spi1,
  189. },
  190. };
  191. #endif /*RT_USING_SPI1*/
  192. #ifdef RT_USING_SPI2
  193. static struct stm32_hw_spi spi2hwdata = {
  194. .Instance = SPI2,
  195. };
  196. const struct stm32_spi spi2 = {
  197. SPI2,
  198. &spi2hwdata,
  199. };
  200. rt_err_t spi2configure(struct rt_spi_device *device,
  201. struct rt_spi_configuration *configuration)
  202. {
  203. spi2hwdata.cfg = configuration;
  204. return stml0xx_spi_init(spi2.spi_device, configuration);
  205. }
  206. const struct rt_spi_ops stm_spi_ops2 =
  207. {
  208. .configure = spi2configure,
  209. .xfer = spixfer,
  210. };
  211. const static struct stm32_hw_spi_cs stm32_spi2_cs = {
  212. SPI2PINNSS,
  213. };
  214. static struct rt_spi_bus stm_spi_bus2 = {
  215. .parent = {
  216. .user_data = (void *)&spi2,
  217. },
  218. };
  219. #endif /*RT_USING_SPI2*/
  220. static void RCC_Configuration(void)
  221. {
  222. #ifdef RT_USING_SPI1
  223. __HAL_RCC_SPI1_CLK_ENABLE();
  224. #endif /*RT_USING_SPI1*/
  225. #ifdef RT_USING_SPI2
  226. __HAL_RCC_SPI2_CLK_ENABLE();
  227. #endif /*RT_USING_SPI2*/
  228. }
  229. static void GPIO_Configuration(void)
  230. {
  231. #ifdef RT_USING_SPI1
  232. {
  233. /**SPI1 GPIO Configuration **/
  234. rt_uint32_t mode;
  235. mode = (GPIO_AF0_SPI1 << 8) | GPIO_MODE_AF_PP;
  236. stm32_pin_mode_early(SPI1PINSCK, mode);
  237. stm32_pin_mode_early(SPI1PINMISO, mode);
  238. stm32_pin_mode_early(SPI1PINMOSI, mode);
  239. }
  240. #endif /*RT_USING_SPI1*/
  241. #ifdef RT_USING_SPI2
  242. #endif /*RT_USING_SPI1*/
  243. }
  244. int stm32_hw_spi_init(void)
  245. {
  246. int result1 = RT_EOK, result2 = RT_EOK;
  247. RCC_Configuration();
  248. GPIO_Configuration();
  249. #ifdef RT_USING_SPI1
  250. {
  251. result1 = rt_spi_bus_register(&stm_spi_bus1, "spi1", &stm_spi_ops1);
  252. static struct rt_spi_device spi_device;
  253. rt_uint32_t mode = GPIO_MODE_OUTPUT_PP;
  254. stm32_pin_mode_early(SPI1PINNSS, mode);
  255. stm32_pin_write_early(SPI1PINNSS, 1);
  256. rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void *)&stm32_spi1_cs);
  257. }
  258. #endif /*RT_USING_SPI1*/
  259. #ifdef RT_USING_SPI2
  260. {
  261. result2 = rt_spi_bus_register(&stm_spi_bus2, "spi2", &stm_spi_ops1);
  262. static struct rt_spi_device spi_device;
  263. rt_uint32_t mode = GPIO_MODE_OUTPUT_PP;
  264. stm32_pin_mode_early(SPI2PINNSS, mode);
  265. stm32_pin_write_early(SPI2PINNSS, 1);
  266. rt_spi_bus_attach_device(&spi_device, "spi20", "spi2", (void *)&stm32_spi2_cs);
  267. }
  268. #endif /*RT_USING_SPI2*/
  269. return result1 | result2;
  270. }
  271. INIT_BOARD_EXPORT(stm32_hw_spi_init);
  272. #endif /*RT_USING_SPI*/