drv_spi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-01-24 wangyq the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <string.h>
  13. #include <rthw.h>
  14. #include "board.h"
  15. #include "drv_spi.h"
  16. #include <ald_spi.h>
  17. #include <ald_gpio.h>
  18. #include <ald_cmu.h>
  19. #ifdef RT_USING_SPI
  20. #define SPITIMEOUT 0x0FFF
  21. rt_err_t spi_configure(struct rt_spi_device *device,
  22. struct rt_spi_configuration *cfg)
  23. {
  24. spi_handle_t *hspi;
  25. hspi = (spi_handle_t *)device->bus->parent.user_data;
  26. /* config spi mode */
  27. if (cfg->mode & RT_SPI_SLAVE)
  28. {
  29. hspi->init.mode = SPI_MODE_SLAVER;
  30. }
  31. else
  32. {
  33. hspi->init.mode = SPI_MODE_MASTER;
  34. }
  35. if (cfg->mode & RT_SPI_3WIRE)
  36. {
  37. hspi->init.dir = SPI_DIRECTION_1LINE;
  38. }
  39. else
  40. {
  41. hspi->init.dir = SPI_DIRECTION_2LINES;
  42. }
  43. if (cfg->data_width == 8)
  44. {
  45. hspi->init.data_size = SPI_DATA_SIZE_8;
  46. }
  47. else if (cfg->data_width == 16)
  48. {
  49. hspi->init.data_size = SPI_DATA_SIZE_16;
  50. }
  51. if (cfg->mode & RT_SPI_CPHA)
  52. {
  53. hspi->init.phase = SPI_CPHA_SECOND;
  54. }
  55. else
  56. {
  57. hspi->init.phase = SPI_CPHA_FIRST;
  58. }
  59. if (cfg->mode & RT_SPI_CPOL)
  60. {
  61. hspi->init.polarity = SPI_CPOL_HIGH;
  62. }
  63. else
  64. {
  65. hspi->init.polarity = SPI_CPOL_LOW;
  66. }
  67. if (cfg->mode & RT_SPI_NO_CS)
  68. {
  69. hspi->init.ss_en = DISABLE;
  70. }
  71. else
  72. {
  73. hspi->init.ss_en = ENABLE;
  74. }
  75. /* config spi clock */
  76. if (cfg->max_hz >= cmu_get_pclk1_clock() / 2)
  77. {
  78. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  79. if (cmu_get_pclk1_clock() / 2 <= 10000000)
  80. {
  81. hspi->init.baud = SPI_BAUD_2;
  82. }
  83. else if (cmu_get_pclk1_clock() / 4 <= 10000000)
  84. {
  85. hspi->init.baud = SPI_BAUD_4;
  86. }
  87. else
  88. {
  89. hspi->init.baud = SPI_BAUD_8;
  90. }
  91. }
  92. else if (cfg->max_hz >= cmu_get_pclk1_clock() / 4)
  93. {
  94. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  95. if (cmu_get_pclk1_clock() / 4 <= 10000000)
  96. {
  97. hspi->init.baud = SPI_BAUD_4;
  98. }
  99. else
  100. {
  101. hspi->init.baud = SPI_BAUD_8;
  102. }
  103. }
  104. else if (cfg->max_hz >= cmu_get_pclk1_clock() / 8)
  105. {
  106. hspi->init.baud = SPI_BAUD_8;
  107. }
  108. else if (cfg->max_hz >= cmu_get_pclk1_clock() / 16)
  109. {
  110. hspi->init.baud = SPI_BAUD_16;
  111. }
  112. else if (cfg->max_hz >= cmu_get_pclk1_clock() / 32)
  113. {
  114. hspi->init.baud = SPI_BAUD_32;
  115. }
  116. else if (cfg->max_hz >= cmu_get_pclk1_clock() / 64)
  117. {
  118. hspi->init.baud = SPI_BAUD_64;
  119. }
  120. else if (cfg->max_hz >= cmu_get_pclk1_clock() / 128)
  121. {
  122. hspi->init.baud = SPI_BAUD_128;
  123. }
  124. else
  125. {
  126. hspi->init.baud = SPI_BAUD_256;
  127. }
  128. spi_init(hspi);
  129. return RT_EOK;
  130. }
  131. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  132. {
  133. rt_err_t res;
  134. spi_handle_t *hspi;
  135. struct es32f0_hw_spi_cs *cs;
  136. RT_ASSERT(device != RT_NULL);
  137. RT_ASSERT(device->bus != RT_NULL);
  138. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  139. RT_ASSERT(message->send_buf != RT_NULL || message->recv_buf != RT_NULL);
  140. hspi = (spi_handle_t *)device->bus->parent.user_data;
  141. cs = device->parent.user_data;
  142. /* only send data */
  143. if (message->recv_buf == RT_NULL)
  144. {
  145. if (message->cs_take)
  146. {
  147. rt_pin_write(cs->pin, 0);
  148. }
  149. res = spi_send(hspi, (rt_uint8_t *)message->send_buf, (rt_int32_t)message->length, SPITIMEOUT);
  150. if (message->cs_release)
  151. {
  152. rt_pin_write(cs->pin, 1);
  153. }
  154. if (res != RT_EOK)
  155. return RT_ERROR;
  156. }
  157. /* only receive data */
  158. if (message->send_buf == RT_NULL)
  159. {
  160. if (message->cs_take)
  161. {
  162. rt_pin_write(cs->pin, 0);
  163. }
  164. res = spi_recv(hspi, (rt_uint8_t *)message->recv_buf, (rt_int32_t)message->length, SPITIMEOUT);
  165. if (message->cs_release)
  166. {
  167. rt_pin_write(cs->pin, 1);
  168. }
  169. if (res != RT_EOK)
  170. return RT_ERROR;
  171. }
  172. /* send & receive */
  173. else
  174. {
  175. if (message->cs_take)
  176. {
  177. rt_pin_write(cs->pin, 0);
  178. }
  179. res = spi_send_recv(hspi, (rt_uint8_t *)message->send_buf, (rt_uint8_t *)message->recv_buf,
  180. (rt_int32_t)message->length, SPITIMEOUT);
  181. if (message->cs_release)
  182. {
  183. rt_pin_write(cs->pin, 1);
  184. }
  185. if (res != RT_EOK)
  186. return RT_ERROR;
  187. }
  188. return message->length;
  189. }
  190. const struct rt_spi_ops es32f0_spi_ops =
  191. {
  192. spi_configure,
  193. spixfer,
  194. };
  195. static struct rt_spi_bus _spi_bus0, _spi_bus1;
  196. static spi_handle_t _spi0, _spi1;
  197. int es32f0_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
  198. {
  199. struct rt_spi_bus *spi_bus;
  200. spi_handle_t *spi;
  201. gpio_init_t gpio_instruct;
  202. if (SPIx == SPI0)
  203. {
  204. _spi0.perh = SPI0;
  205. spi_bus = &_spi_bus0;
  206. spi = &_spi0;
  207. /* SPI0 gpio init */
  208. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  209. gpio_instruct.odos = GPIO_PUSH_PULL;
  210. gpio_instruct.func = GPIO_FUNC_4;
  211. gpio_instruct.type = GPIO_TYPE_CMOS;
  212. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  213. /* PB3->SPI0_SCK, PB5->SPI0_MOSI */
  214. gpio_init(GPIOB, GPIO_PIN_3 | GPIO_PIN_5, &gpio_instruct);
  215. /* PB4->SPI0_MISO */
  216. gpio_instruct.mode = GPIO_MODE_INPUT;
  217. gpio_init(GPIOB, GPIO_PIN_4, &gpio_instruct);
  218. }
  219. else if (SPIx == SPI1)
  220. {
  221. _spi1.perh = SPI1;
  222. spi_bus = &_spi_bus1;
  223. spi = &_spi1;
  224. /* SPI1 gpio init */
  225. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  226. gpio_instruct.odos = GPIO_PUSH_PULL;
  227. gpio_instruct.func = GPIO_FUNC_4;
  228. gpio_instruct.type = GPIO_TYPE_CMOS;
  229. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  230. /* PB13->SPI1_SCK, PB15->SPI1_MOSI */
  231. gpio_init(GPIOB, GPIO_PIN_13 | GPIO_PIN_15, &gpio_instruct);
  232. /* PB14->SPI1_MISO */
  233. gpio_instruct.mode = GPIO_MODE_INPUT;
  234. gpio_init(GPIOB, GPIO_PIN_14, &gpio_instruct);
  235. }
  236. else
  237. {
  238. return -1;
  239. }
  240. spi_bus->parent.user_data = spi;
  241. return rt_spi_bus_register(spi_bus, name, &es32f0_spi_ops);
  242. }
  243. rt_err_t es32f0_spi_device_attach(rt_uint32_t pin, const char *bus_name, const char *device_name)
  244. {
  245. /* define spi Instance */
  246. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  247. RT_ASSERT(spi_device != RT_NULL);
  248. struct es32f0_hw_spi_cs *cs_pin = (struct es32f0_hw_spi_cs *)rt_malloc(sizeof(struct es32f0_hw_spi_cs));
  249. RT_ASSERT(cs_pin != RT_NULL);
  250. cs_pin->pin = pin;
  251. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  252. rt_pin_write(pin, 1);
  253. return rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  254. }
  255. int rt_hw_spi_init(void)
  256. {
  257. int result = 0;
  258. #ifdef BSP_USING_SPI0
  259. result = es32f0_spi_register_bus(SPI0, "spi0");
  260. #endif
  261. #ifdef BSP_USING_SPI1
  262. result = es32f0_spi_register_bus(SPI1, "spi1");
  263. #endif
  264. return result;
  265. }
  266. INIT_BOARD_EXPORT(rt_hw_spi_init);
  267. #endif