drv_spi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * 2020-01-14 wangyq the first version
  9. * 2019-11-01 wangyq update libraries
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <string.h>
  14. #include <rthw.h>
  15. #include "board.h"
  16. #include "drv_spi.h"
  17. #include <ald_spi.h>
  18. #include <ald_gpio.h>
  19. #include <ald_cmu.h>
  20. #ifdef RT_USING_SPI
  21. #define SPITIMEOUT 0xFFFF
  22. rt_err_t spi_configure(struct rt_spi_device *device,
  23. struct rt_spi_configuration *cfg)
  24. {
  25. spi_handle_t *hspi;
  26. hspi = (spi_handle_t *)device->bus->parent.user_data;
  27. hspi->init.ss_en = DISABLE;
  28. hspi->init.crc_calc = DISABLE;
  29. /* config spi mode */
  30. if (cfg->mode & RT_SPI_SLAVE)
  31. {
  32. hspi->init.mode = SPI_MODE_SLAVER;
  33. }
  34. else
  35. {
  36. hspi->init.mode = SPI_MODE_MASTER;
  37. }
  38. if (cfg->mode & RT_SPI_3WIRE)
  39. {
  40. hspi->init.dir = SPI_DIRECTION_1LINE;
  41. }
  42. else
  43. {
  44. hspi->init.dir = SPI_DIRECTION_2LINES;
  45. }
  46. if (cfg->data_width == 8)
  47. {
  48. hspi->init.data_size = SPI_DATA_SIZE_8;
  49. }
  50. else if (cfg->data_width == 16)
  51. {
  52. hspi->init.data_size = SPI_DATA_SIZE_16;
  53. }
  54. if (cfg->mode & RT_SPI_CPHA)
  55. {
  56. hspi->init.phase = SPI_CPHA_SECOND;
  57. }
  58. else
  59. {
  60. hspi->init.phase = SPI_CPHA_FIRST;
  61. }
  62. if (cfg->mode & RT_SPI_MSB)
  63. {
  64. hspi->init.first_bit = SPI_FIRSTBIT_MSB;
  65. }
  66. else
  67. {
  68. hspi->init.first_bit = SPI_FIRSTBIT_LSB;
  69. }
  70. if (cfg->mode & RT_SPI_CPOL)
  71. {
  72. hspi->init.polarity = SPI_CPOL_HIGH;
  73. }
  74. else
  75. {
  76. hspi->init.polarity = SPI_CPOL_LOW;
  77. }
  78. if (cfg->mode & RT_SPI_NO_CS)
  79. {
  80. hspi->init.ss_en = DISABLE;
  81. }
  82. else
  83. {
  84. hspi->init.ss_en = ENABLE;
  85. }
  86. /* config spi clock */
  87. if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 2)
  88. {
  89. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  90. if (ald_cmu_get_pclk1_clock() / 2 <= 10000000)
  91. {
  92. hspi->init.baud = SPI_BAUD_2;
  93. }
  94. else if (ald_cmu_get_pclk1_clock() / 4 <= 10000000)
  95. {
  96. hspi->init.baud = SPI_BAUD_4;
  97. }
  98. else
  99. {
  100. hspi->init.baud = SPI_BAUD_8;
  101. }
  102. }
  103. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 4)
  104. {
  105. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  106. if (ald_cmu_get_pclk1_clock() / 4 <= 10000000)
  107. {
  108. hspi->init.baud = SPI_BAUD_4;
  109. }
  110. else
  111. {
  112. hspi->init.baud = SPI_BAUD_8;
  113. }
  114. }
  115. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 8)
  116. {
  117. hspi->init.baud = SPI_BAUD_8;
  118. }
  119. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 16)
  120. {
  121. hspi->init.baud = SPI_BAUD_16;
  122. }
  123. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 32)
  124. {
  125. hspi->init.baud = SPI_BAUD_32;
  126. }
  127. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 64)
  128. {
  129. hspi->init.baud = SPI_BAUD_64;
  130. }
  131. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 128)
  132. {
  133. hspi->init.baud = SPI_BAUD_128;
  134. }
  135. else
  136. {
  137. hspi->init.baud = SPI_BAUD_256;
  138. }
  139. ald_spi_init(hspi);
  140. return RT_EOK;
  141. }
  142. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  143. {
  144. rt_err_t res;
  145. spi_handle_t *hspi;
  146. struct es32f3_hw_spi_cs *cs;
  147. RT_ASSERT(device != RT_NULL);
  148. RT_ASSERT(device->bus != RT_NULL);
  149. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  150. hspi = (spi_handle_t *)device->bus->parent.user_data;
  151. cs = device->parent.user_data;
  152. if(message->send_buf != RT_NULL || message->recv_buf != RT_NULL)
  153. {
  154. /* send & receive */
  155. if ((message->send_buf != RT_NULL) && (message->recv_buf != RT_NULL))
  156. {
  157. if (message->cs_take)
  158. {
  159. rt_pin_write(cs->pin, 0);
  160. }
  161. res = ald_spi_send_recv(hspi, (rt_uint8_t *)message->send_buf, (rt_uint8_t *)message->recv_buf,
  162. (rt_int32_t)message->length, SPITIMEOUT);
  163. if (message->cs_release)
  164. {
  165. rt_pin_write(cs->pin, 1);
  166. }
  167. if (res != RT_EOK)
  168. return RT_ERROR;
  169. }
  170. else
  171. {
  172. /* only send data */
  173. if (message->recv_buf == RT_NULL)
  174. {
  175. if (message->cs_take)
  176. {
  177. rt_pin_write(cs->pin, 0);
  178. }
  179. res = ald_spi_send(hspi, (rt_uint8_t *)message->send_buf, (rt_int32_t)message->length, SPITIMEOUT);
  180. if (message->cs_release)
  181. {
  182. rt_pin_write(cs->pin, 1);
  183. }
  184. if (res != RT_EOK)
  185. return RT_ERROR;
  186. }
  187. /* only receive data */
  188. if (message->send_buf == RT_NULL)
  189. {
  190. if (message->cs_take)
  191. {
  192. rt_pin_write(cs->pin, 0);
  193. }
  194. res = ald_spi_recv(hspi, (rt_uint8_t *)message->recv_buf, (rt_int32_t)message->length, SPITIMEOUT);
  195. if (message->cs_release)
  196. {
  197. rt_pin_write(cs->pin, 1);
  198. }
  199. if (res != RT_EOK)
  200. return RT_ERROR;
  201. }
  202. }
  203. }
  204. else
  205. {
  206. if (message->cs_take)
  207. {
  208. rt_pin_write(cs->pin, 0);
  209. }
  210. if (message->cs_release)
  211. {
  212. rt_pin_write(cs->pin, 1);
  213. }
  214. return RT_EOK;
  215. }
  216. return message->length;
  217. }
  218. const struct rt_spi_ops es32f3_spi_ops =
  219. {
  220. spi_configure,
  221. spixfer,
  222. };
  223. rt_err_t es32f3_spi_device_attach(rt_uint32_t pin, const char *bus_name, const char *device_name)
  224. {
  225. /* define spi Instance */
  226. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  227. RT_ASSERT(spi_device != RT_NULL);
  228. struct es32f3_hw_spi_cs *cs_pin = (struct es32f3_hw_spi_cs *)rt_malloc(sizeof(struct es32f3_hw_spi_cs));
  229. RT_ASSERT(cs_pin != RT_NULL);
  230. cs_pin->pin = pin;
  231. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  232. rt_pin_write(pin, 1);
  233. return rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  234. }
  235. #ifdef BSP_USING_SPI0
  236. static struct rt_spi_bus _spi_bus0;
  237. static spi_handle_t _spi0;
  238. #endif
  239. #ifdef BSP_USING_SPI1
  240. static struct rt_spi_bus _spi_bus1;
  241. static spi_handle_t _spi1;
  242. #endif
  243. #ifdef BSP_USING_SPI2
  244. static struct rt_spi_bus _spi_bus2;
  245. static spi_handle_t _spi2;
  246. #endif
  247. int rt_hw_spi_init(void)
  248. {
  249. int result = RT_EOK;
  250. struct rt_spi_bus *spi_bus;
  251. spi_handle_t *spi;
  252. gpio_init_t gpio_instruct;
  253. #ifdef BSP_USING_SPI0
  254. _spi0.perh = SPI0;
  255. spi_bus = &_spi_bus0;
  256. spi = &_spi0;
  257. rt_device_t spi_bus_dev0;
  258. /* SPI0 gpio init */
  259. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  260. gpio_instruct.odos = GPIO_PUSH_PULL;
  261. gpio_instruct.podrv = GPIO_OUT_DRIVE_1;
  262. gpio_instruct.nodrv = GPIO_OUT_DRIVE_1;
  263. gpio_instruct.func = GPIO_FUNC_4;
  264. gpio_instruct.type = GPIO_TYPE_TTL;
  265. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  266. /* PB3->SPI0_SCK, PB5->SPI0_MOSI */
  267. ald_gpio_init(GPIOB, GPIO_PIN_3 | GPIO_PIN_5, &gpio_instruct);
  268. /* PB4->SPI0_MISO */
  269. gpio_instruct.mode = GPIO_MODE_INPUT;
  270. ald_gpio_init(GPIOB, GPIO_PIN_4, &gpio_instruct);
  271. spi_bus->parent.user_data = spi;
  272. result = rt_spi_bus_register(spi_bus, "spi0", &es32f3_spi_ops);
  273. if (result != RT_EOK)
  274. {
  275. return result;
  276. }
  277. rt_device_register(spi_bus_dev0, "spi00", RT_DEVICE_FLAG_RDWR);
  278. /* SPI0_NSS = PA15 = PIN 50 */
  279. result = es32f3_spi_device_attach(50, "spi0", "spi00");
  280. if (result != RT_EOK)
  281. {
  282. return result;
  283. }
  284. #endif
  285. #ifdef BSP_USING_SPI1
  286. _spi1.perh = SPI1;
  287. spi_bus = &_spi_bus1;
  288. spi = &_spi1;
  289. rt_device_t spi_bus_dev0;
  290. /* SPI1 gpio init */
  291. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  292. gpio_instruct.odos = GPIO_PUSH_PULL;
  293. gpio_instruct.podrv = GPIO_OUT_DRIVE_1;
  294. gpio_instruct.nodrv = GPIO_OUT_DRIVE_1;
  295. gpio_instruct.func = GPIO_FUNC_4;
  296. gpio_instruct.type = GPIO_TYPE_TTL;
  297. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  298. /* PC01->SPI1_SCK, PC03->SPI1_MOSI */
  299. ald_gpio_init(GPIOC, GPIO_PIN_1 | GPIO_PIN_3, &gpio_instruct);
  300. /* PC02->SPI1_MISO */
  301. gpio_instruct.mode = GPIO_MODE_INPUT;
  302. ald_gpio_init(GPIOC, GPIO_PIN_2, &gpio_instruct);
  303. spi_bus->parent.user_data = spi;
  304. result = rt_spi_bus_register(spi_bus, "spi1", &es32f3_spi_ops);
  305. if (result != RT_EOK)
  306. {
  307. return result;
  308. }
  309. rt_device_register(spi_bus_dev0, "spi10", RT_DEVICE_FLAG_RDWR);
  310. /* SPI1_NSS = PC00 = PIN 8 */
  311. result = es32f3_spi_device_attach(8, "spi1", "spi10");
  312. if (result != RT_EOK)
  313. {
  314. return result;
  315. }
  316. #endif
  317. #ifdef BSP_USING_SPI2
  318. _spi1.perh = SPI2;
  319. spi_bus = &_spi_bus2;
  320. spi = &_spi2;
  321. rt_device_t spi_bus_dev0;
  322. /* SPI2 gpio init */
  323. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  324. gpio_instruct.odos = GPIO_PUSH_PULL;
  325. gpio_instruct.podrv = GPIO_OUT_DRIVE_1;
  326. gpio_instruct.nodrv = GPIO_OUT_DRIVE_1;
  327. gpio_instruct.func = GPIO_FUNC_5;
  328. gpio_instruct.type = GPIO_TYPE_TTL;
  329. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  330. /* PC05->SPI1_SCK, PB01->SPI1_MOSI */
  331. ald_gpio_init(GPIOC, GPIO_PIN_5 | GPIO_PIN_1, &gpio_instruct);
  332. /* PB00->SPI1_MISO */
  333. gpio_instruct.mode = GPIO_MODE_INPUT;
  334. ald_gpio_init(GPIOB, GPIO_PIN_0, &gpio_instruct);
  335. spi_bus->parent.user_data = spi;
  336. result = rt_spi_bus_register(spi_bus, "spi2", &es32f3_spi_ops);
  337. if (result != RT_EOK)
  338. {
  339. return result;
  340. }
  341. rt_device_register(spi_bus_dev0, "spi20", RT_DEVICE_FLAG_RDWR);
  342. /* SPI2_NSS = PC04 = PIN 24 */
  343. result = es32f3_spi_device_attach(39, "spi2", "spi20");
  344. if (result != RT_EOK)
  345. {
  346. return result;
  347. }
  348. #endif
  349. return result;
  350. }
  351. INIT_BOARD_EXPORT(rt_hw_spi_init);
  352. #endif