drv_spi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-31 ZYH first version
  9. * 2018-12-10 Zohar_Lee format file
  10. * 2020-07-10 lik format file
  11. */
  12. #include "drv_spi.h"
  13. #ifdef RT_USING_SPI
  14. #ifdef BSP_USING_SPI
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.spi"
  17. #include <drv_log.h>
  18. static struct swm_spi_cfg spi_cfg[] =
  19. {
  20. #ifdef BSP_USING_SPI0
  21. SPI0_BUS_CONFIG,
  22. #endif
  23. #ifdef BSP_USING_SPI1
  24. SPI1_BUS_CONFIG,
  25. #endif
  26. };
  27. static struct swm_spi spi_bus_drv[sizeof(spi_cfg) / sizeof(spi_cfg[0])] = {0};
  28. static rt_err_t swm_spi_init(struct swm_spi *spi_drv, struct rt_spi_configuration *configure)
  29. {
  30. RT_ASSERT(spi_drv != RT_NULL);
  31. RT_ASSERT(configure != RT_NULL);
  32. struct swm_spi_cfg *cfg = spi_drv->cfg;
  33. if (configure->mode & RT_SPI_SLAVE)
  34. {
  35. cfg->spi_initstruct.Master = 0;
  36. }
  37. else
  38. {
  39. cfg->spi_initstruct.Master = 1;
  40. }
  41. if (configure->mode & RT_SPI_3WIRE)
  42. {
  43. return RT_EINVAL;
  44. }
  45. if (configure->data_width == 8)
  46. {
  47. cfg->spi_initstruct.WordSize = 8;
  48. }
  49. else if (configure->data_width == 16)
  50. {
  51. cfg->spi_initstruct.WordSize = 16;
  52. }
  53. else
  54. {
  55. return RT_EIO;
  56. }
  57. if (configure->mode & RT_SPI_CPHA)
  58. {
  59. cfg->spi_initstruct.SampleEdge = SPI_SECOND_EDGE;
  60. }
  61. else
  62. {
  63. cfg->spi_initstruct.SampleEdge = SPI_FIRST_EDGE;
  64. }
  65. if (configure->mode & RT_SPI_CPOL)
  66. {
  67. cfg->spi_initstruct.IdleLevel = SPI_HIGH_LEVEL;
  68. }
  69. else
  70. {
  71. cfg->spi_initstruct.IdleLevel = SPI_LOW_LEVEL;
  72. }
  73. if (configure->max_hz >= SystemCoreClock / 4)
  74. {
  75. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_4;
  76. }
  77. else if (configure->max_hz >= SystemCoreClock / 8)
  78. {
  79. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_8;
  80. }
  81. else if (configure->max_hz >= SystemCoreClock / 16)
  82. {
  83. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_16;
  84. }
  85. else if (configure->max_hz >= SystemCoreClock / 32)
  86. {
  87. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_32;
  88. }
  89. else if (configure->max_hz >= SystemCoreClock / 64)
  90. {
  91. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_64;
  92. }
  93. else if (configure->max_hz >= SystemCoreClock / 128)
  94. {
  95. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_128;
  96. }
  97. else if (configure->max_hz >= SystemCoreClock / 256)
  98. {
  99. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_256;
  100. }
  101. else
  102. {
  103. /* min prescaler 512 */
  104. cfg->spi_initstruct.clkDiv = SPI_CLKDIV_512;
  105. }
  106. SPI_Init(cfg->SPIx, &(cfg->spi_initstruct));
  107. SPI_Open(cfg->SPIx);
  108. LOG_D("%s init done", cfg->name);
  109. return RT_EOK;
  110. }
  111. #define SPISTEP(datalen) (((datalen) == 8) ? 1 : 2)
  112. #define SPISEND_1(reg, ptr, datalen) \
  113. do \
  114. { \
  115. if (datalen == 8) \
  116. { \
  117. (reg) = *(rt_uint8_t *)(ptr); \
  118. } \
  119. else \
  120. { \
  121. (reg) = *(rt_uint16_t *)(ptr); \
  122. } \
  123. } while (0)
  124. #define SPIRECV_1(reg, ptr, datalen) \
  125. do \
  126. { \
  127. if (datalen == 8) \
  128. { \
  129. *(rt_uint8_t *)(ptr) = (reg); \
  130. } \
  131. else \
  132. { \
  133. *(rt_uint16_t *)(ptr) = reg; \
  134. } \
  135. } while (0)
  136. static rt_err_t spitxrx1b(struct swm_spi *spi_drv, void *rcvb, const void *sndb)
  137. {
  138. rt_uint32_t padrcv = 0;
  139. rt_uint32_t padsnd = 0xFF;
  140. if (!rcvb && !sndb)
  141. {
  142. return RT_ERROR;
  143. }
  144. if (!rcvb)
  145. {
  146. rcvb = &padrcv;
  147. }
  148. if (!sndb)
  149. {
  150. sndb = &padsnd;
  151. }
  152. while (SPI_IsTXFull(spi_drv->cfg->SPIx))
  153. ;
  154. SPISEND_1(spi_drv->cfg->SPIx->DATA, sndb, spi_drv->cfg->spi_initstruct.WordSize);
  155. while (SPI_IsRXEmpty(spi_drv->cfg->SPIx))
  156. ;
  157. SPIRECV_1(spi_drv->cfg->SPIx->DATA, rcvb, spi_drv->cfg->spi_initstruct.WordSize);
  158. return RT_EOK;
  159. }
  160. static rt_uint32_t swm_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  161. {
  162. rt_err_t res;
  163. RT_ASSERT(device != RT_NULL);
  164. RT_ASSERT(device->bus != RT_NULL);
  165. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  166. RT_ASSERT(message != RT_NULL);
  167. struct swm_spi *spi_drv = rt_container_of(device->bus, struct swm_spi, spi_bus);
  168. struct swm_spi_cfg *cfg = spi_drv->cfg;
  169. struct swm_spi_cs *cs = device->parent.user_data;
  170. if (message->cs_take)
  171. {
  172. GPIO_ClrBit(cs->GPIOx, cs->gpio_pin);
  173. }
  174. LOG_D("%s transfer prepare and start", cfg->name);
  175. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d",
  176. cfg->name, (uint32_t)message->send_buf, (uint32_t)message->recv_buf, message->length);
  177. const rt_uint8_t *sndb = message->send_buf;
  178. rt_uint8_t *rcvb = message->recv_buf;
  179. rt_int32_t length = message->length;
  180. while (length)
  181. {
  182. res = spitxrx1b(spi_drv, rcvb, sndb);
  183. if (rcvb)
  184. {
  185. rcvb += SPISTEP(cfg->spi_initstruct.WordSize);
  186. }
  187. if (sndb)
  188. {
  189. sndb += SPISTEP(cfg->spi_initstruct.WordSize);
  190. }
  191. if (res != RT_EOK)
  192. {
  193. break;
  194. }
  195. length--;
  196. }
  197. /* Wait until Busy flag is reset before disabling SPI */
  198. while (!SPI_IsTXEmpty(cfg->SPIx) && !SPI_IsRXEmpty(cfg->SPIx))
  199. ;
  200. if (message->cs_release)
  201. {
  202. GPIO_SetBit(cs->GPIOx, cs->gpio_pin);
  203. }
  204. return message->length - length;
  205. }
  206. static rt_err_t swm_spi_configure(struct rt_spi_device *device,
  207. struct rt_spi_configuration *configure)
  208. {
  209. RT_ASSERT(device != RT_NULL);
  210. RT_ASSERT(configure != RT_NULL);
  211. struct swm_spi *spi_drv = rt_container_of(device->bus, struct swm_spi, spi_bus);
  212. spi_drv->configure = configure;
  213. return swm_spi_init(spi_drv, configure);
  214. }
  215. const static struct rt_spi_ops swm_spi_ops =
  216. {
  217. .configure = swm_spi_configure,
  218. .xfer = swm_spi_xfer,
  219. };
  220. //cannot be used before completion init
  221. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint32_t cs_gpio_pin)
  222. {
  223. RT_ASSERT(bus_name != RT_NULL);
  224. RT_ASSERT(device_name != RT_NULL);
  225. rt_err_t result;
  226. struct rt_spi_device *spi_device;
  227. struct swm_spi_cs *cs_pin;
  228. GPIO_Init(cs_gpiox, cs_gpio_pin, 1, 0, 0);
  229. GPIO_SetBit(cs_gpiox, cs_gpio_pin);
  230. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  231. RT_ASSERT(spi_device != RT_NULL);
  232. cs_pin = (struct swm_spi_cs *)rt_malloc(sizeof(struct swm_spi_cs));
  233. RT_ASSERT(cs_pin != RT_NULL);
  234. cs_pin->GPIOx = cs_gpiox;
  235. cs_pin->gpio_pin = cs_gpio_pin;
  236. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  237. if (result != RT_EOK)
  238. {
  239. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  240. }
  241. RT_ASSERT(result == RT_EOK);
  242. LOG_D("%s attach to %s done", device_name, bus_name);
  243. return result;
  244. }
  245. int rt_hw_spi_init(void)
  246. {
  247. rt_err_t result;
  248. #ifdef BSP_USING_SPI0
  249. PORT_Init(PORTP, PIN23, FUNMUX1_SPI0_SCLK, 0);
  250. PORT_Init(PORTP, PIN18, FUNMUX0_SPI0_MOSI, 0);
  251. PORT_Init(PORTP, PIN19, FUNMUX1_SPI0_MISO, 1);
  252. #endif //BSP_USING_SPI0
  253. #ifdef BSP_USING_SPI1
  254. PORT_Init(PORTB, PIN1, FUNMUX1_SPI1_SCLK, 0);
  255. PORT_Init(PORTB, PIN2, FUNMUX0_SPI1_MOSI, 0);
  256. PORT_Init(PORTB, PIN3, FUNMUX1_SPI1_MISO, 1);
  257. #endif //BSP_USING_SPI1
  258. for (int i = 0; i < sizeof(spi_cfg) / sizeof(spi_cfg[0]); i++)
  259. {
  260. spi_bus_drv[i].cfg = &spi_cfg[i];
  261. spi_bus_drv[i].spi_bus.parent.user_data = &spi_cfg[i];
  262. result = rt_spi_bus_register(&spi_bus_drv[i].spi_bus, spi_cfg[i].name, &swm_spi_ops);
  263. RT_ASSERT(result == RT_EOK);
  264. LOG_D("%s bus init done", spi_config[i].bus_name);
  265. }
  266. return result;
  267. }
  268. INIT_BOARD_EXPORT(rt_hw_spi_init);
  269. #endif /* BSP_USING_SPI */
  270. #endif /* RT_USING_SPI */