drv_spi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2019-01-24 wangyq the first version
  21. * 2019-11-01 wangyq update libraries
  22. * 2021-04-20 liuhy the second version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <string.h>
  27. #include <rthw.h>
  28. #include "board.h"
  29. #include "drv_spi.h"
  30. #ifdef RT_USING_SPI
  31. #define SPITIMEOUT 0x0FFF
  32. rt_err_t spi_configure(struct rt_spi_device *device,
  33. struct rt_spi_configuration *cfg)
  34. {
  35. spi_handle_t *hspi;
  36. hspi = (spi_handle_t *)device->bus->parent.user_data;
  37. hspi->init.ss_en = DISABLE;
  38. hspi->init.crc_calc = DISABLE;
  39. /* config spi mode */
  40. if (cfg->mode & RT_SPI_SLAVE)
  41. {
  42. hspi->init.mode = SPI_MODE_SLAVER;
  43. }
  44. else
  45. {
  46. hspi->init.mode = SPI_MODE_MASTER;
  47. }
  48. if (cfg->mode & RT_SPI_3WIRE)
  49. {
  50. hspi->init.dir = SPI_DIRECTION_1LINE;
  51. }
  52. else
  53. {
  54. hspi->init.dir = SPI_DIRECTION_2LINES;
  55. }
  56. if (cfg->data_width == 8)
  57. {
  58. hspi->init.data_size = SPI_DATA_SIZE_8;
  59. }
  60. else if (cfg->data_width == 16)
  61. {
  62. hspi->init.data_size = SPI_DATA_SIZE_16;
  63. }
  64. if (cfg->mode & RT_SPI_CPHA)
  65. {
  66. hspi->init.phase = SPI_CPHA_SECOND;
  67. }
  68. else
  69. {
  70. hspi->init.phase = SPI_CPHA_FIRST;
  71. }
  72. if (cfg->mode & RT_SPI_MSB)
  73. {
  74. hspi->init.first_bit = SPI_FIRSTBIT_MSB;
  75. }
  76. else
  77. {
  78. hspi->init.first_bit = SPI_FIRSTBIT_LSB;
  79. }
  80. if (cfg->mode & RT_SPI_CPOL)
  81. {
  82. hspi->init.polarity = SPI_CPOL_HIGH;
  83. }
  84. else
  85. {
  86. hspi->init.polarity = SPI_CPOL_LOW;
  87. }
  88. if (cfg->mode & RT_SPI_NO_CS)
  89. {
  90. hspi->init.ss_en = DISABLE;
  91. }
  92. else
  93. {
  94. hspi->init.ss_en = ENABLE;
  95. }
  96. /* config spi clock */
  97. if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 2)
  98. {
  99. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  100. if (ald_cmu_get_pclk1_clock() / 2 <= 10000000)
  101. {
  102. hspi->init.baud = SPI_BAUD_2;
  103. }
  104. else if (ald_cmu_get_pclk1_clock() / 4 <= 10000000)
  105. {
  106. hspi->init.baud = SPI_BAUD_4;
  107. }
  108. else
  109. {
  110. hspi->init.baud = SPI_BAUD_8;
  111. }
  112. }
  113. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 4)
  114. {
  115. /* pclk1 max speed 48MHz, spi master max speed 10MHz */
  116. if (ald_cmu_get_pclk1_clock() / 4 <= 10000000)
  117. {
  118. hspi->init.baud = SPI_BAUD_4;
  119. }
  120. else
  121. {
  122. hspi->init.baud = SPI_BAUD_8;
  123. }
  124. }
  125. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 8)
  126. {
  127. hspi->init.baud = SPI_BAUD_8;
  128. }
  129. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 16)
  130. {
  131. hspi->init.baud = SPI_BAUD_16;
  132. }
  133. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 32)
  134. {
  135. hspi->init.baud = SPI_BAUD_32;
  136. }
  137. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 64)
  138. {
  139. hspi->init.baud = SPI_BAUD_64;
  140. }
  141. else if (cfg->max_hz >= ald_cmu_get_pclk1_clock() / 128)
  142. {
  143. hspi->init.baud = SPI_BAUD_128;
  144. }
  145. else
  146. {
  147. hspi->init.baud = SPI_BAUD_256;
  148. }
  149. ald_spi_init(hspi);
  150. return RT_EOK;
  151. }
  152. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  153. {
  154. rt_err_t res;
  155. spi_handle_t *hspi;
  156. struct es32f0_hw_spi_cs *cs;
  157. RT_ASSERT(device != RT_NULL);
  158. RT_ASSERT(device->bus != RT_NULL);
  159. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  160. hspi = (spi_handle_t *)device->bus->parent.user_data;
  161. cs = device->parent.user_data;
  162. if (message->cs_take)
  163. {
  164. rt_pin_write(cs->pin, ES_SPI_CS_LEVEL);
  165. }
  166. if(message->send_buf != RT_NULL || message->recv_buf != RT_NULL)
  167. {
  168. /* send & receive */
  169. if ((message->send_buf != RT_NULL) && (message->recv_buf != RT_NULL))
  170. {
  171. res = ald_spi_send_recv(hspi, (rt_uint8_t *)message->send_buf, (rt_uint8_t *)message->recv_buf,
  172. (rt_int32_t)message->length, SPITIMEOUT);
  173. }
  174. else
  175. {
  176. /* only send data */
  177. if (message->recv_buf == RT_NULL)
  178. {
  179. res = ald_spi_send(hspi, (rt_uint8_t *)message->send_buf, (rt_int32_t)message->length, SPITIMEOUT);
  180. }
  181. /* only receive data */
  182. if (message->send_buf == RT_NULL)
  183. {
  184. res = ald_spi_recv(hspi, (rt_uint8_t *)message->recv_buf, (rt_int32_t)message->length, SPITIMEOUT);
  185. }
  186. }
  187. if (message->cs_release)
  188. {
  189. rt_pin_write(cs->pin, !ES_SPI_CS_LEVEL);
  190. }
  191. if (res != RT_EOK)
  192. return -RT_ERROR;
  193. else
  194. return message->length;
  195. }
  196. else
  197. {
  198. if (message->cs_release)
  199. {
  200. rt_pin_write(cs->pin, !ES_SPI_CS_LEVEL);
  201. }
  202. return RT_EOK;
  203. }
  204. }
  205. const struct rt_spi_ops es32f0_spi_ops =
  206. {
  207. spi_configure,
  208. spixfer,
  209. };
  210. rt_err_t es32f0_spi_device_attach(rt_uint32_t pin, const char *bus_name, const char *device_name)
  211. {
  212. int result;
  213. /* define spi Instance */
  214. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  215. RT_ASSERT(spi_device != RT_NULL);
  216. struct es32f0_hw_spi_cs *cs_pin = (struct es32f0_hw_spi_cs *)rt_malloc(sizeof(struct es32f0_hw_spi_cs));
  217. RT_ASSERT(cs_pin != RT_NULL);
  218. cs_pin->pin = pin;
  219. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  220. rt_pin_write(pin, 1);
  221. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  222. #ifdef BSP_USING_SPI0
  223. if(!(strcmp(bus_name,ES_DEVICE_NAME_SPI0_BUS)))SPI_BUS_CONFIG(spi_device->config,0);
  224. #endif
  225. #ifdef BSP_USING_SPI1
  226. if(!(strcmp(bus_name,ES_DEVICE_NAME_SPI1_BUS)))SPI_BUS_CONFIG(spi_device->config,1);
  227. #endif
  228. return result;
  229. }
  230. #ifdef BSP_USING_SPI0
  231. static struct rt_spi_bus _spi_bus0;
  232. static spi_handle_t _spi0;
  233. #endif
  234. #ifdef BSP_USING_SPI1
  235. static struct rt_spi_bus _spi_bus1;
  236. static spi_handle_t _spi1;
  237. #endif
  238. int rt_hw_spi_init(void)
  239. {
  240. int result = RT_EOK;
  241. struct rt_spi_bus *spi_bus;
  242. spi_handle_t *spi;
  243. gpio_init_t gpio_instruct;
  244. gpio_instruct.odos = GPIO_PUSH_PULL;
  245. gpio_instruct.type = GPIO_TYPE_CMOS;
  246. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  247. gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
  248. #ifdef BSP_USING_SPI0
  249. _spi0.perh = SPI0;
  250. spi_bus = &_spi_bus0;
  251. spi = &_spi0;
  252. /* SPI0 gpio init */
  253. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  254. #if defined(ES_SPI0_SCK_GPIO_FUNC)&&defined(ES_SPI0_SCK_GPIO_PORT)&&defined(ES_SPI0_SCK_GPIO_PIN)
  255. gpio_instruct.func = ES_SPI0_SCK_GPIO_FUNC;
  256. ald_gpio_init(ES_SPI0_SCK_GPIO_PORT, ES_SPI0_SCK_GPIO_PIN, &gpio_instruct);
  257. #endif
  258. #if defined(ES_SPI0_MOSI_GPIO_FUNC)&&defined(ES_SPI0_MOSI_GPIO_PORT)&&defined(ES_SPI0_MOSI_GPIO_PIN)
  259. gpio_instruct.func = ES_SPI0_MOSI_GPIO_FUNC;
  260. ald_gpio_init(ES_SPI0_MOSI_GPIO_PORT, ES_SPI0_MOSI_GPIO_PIN, &gpio_instruct);
  261. #endif
  262. gpio_instruct.mode = GPIO_MODE_INPUT;
  263. #if defined(ES_SPI0_MISO_GPIO_FUNC)&&defined(ES_SPI0_MISO_GPIO_PORT)&&defined(ES_SPI0_MISO_GPIO_PIN)
  264. gpio_instruct.func = ES_SPI0_MISO_GPIO_FUNC;
  265. ald_gpio_init(ES_SPI0_MISO_GPIO_PORT, ES_SPI0_MISO_GPIO_PIN, &gpio_instruct);
  266. #endif
  267. spi_bus->parent.user_data = spi;
  268. result = rt_spi_bus_register(spi_bus, ES_DEVICE_NAME_SPI0_BUS, &es32f0_spi_ops);
  269. if (result != RT_EOK)
  270. {
  271. return result;
  272. }
  273. result = es32f0_spi_device_attach(ES_SPI0_NSS_PIN, ES_DEVICE_NAME_SPI0_BUS, ES_DEVICE_NAME_SPI0_DEV0);
  274. if (result != RT_EOK)
  275. {
  276. return result;
  277. }
  278. #endif
  279. #ifdef BSP_USING_SPI1
  280. _spi1.perh = SPI1;
  281. spi_bus = &_spi_bus1;
  282. spi = &_spi1;
  283. /* SPI1 gpio init */
  284. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  285. #if defined(ES_SPI1_SCK_GPIO_FUNC)&&defined(ES_SPI1_SCK_GPIO_PORT)&&defined(ES_SPI1_SCK_GPIO_PIN)
  286. gpio_instruct.func = ES_SPI1_SCK_GPIO_FUNC;
  287. ald_gpio_init(ES_SPI1_SCK_GPIO_PORT, ES_SPI1_SCK_GPIO_PIN, &gpio_instruct);
  288. #endif
  289. #if defined(ES_SPI1_MOSI_GPIO_FUNC)&&defined(ES_SPI1_MOSI_GPIO_PORT)&&defined(ES_SPI1_MOSI_GPIO_PIN)
  290. gpio_instruct.func = ES_SPI1_MOSI_GPIO_FUNC;
  291. ald_gpio_init(ES_SPI1_MOSI_GPIO_PORT, ES_SPI1_MOSI_GPIO_PIN, &gpio_instruct);
  292. #endif
  293. gpio_instruct.mode = GPIO_MODE_INPUT;
  294. #if defined(ES_SPI1_MISO_GPIO_FUNC)&&defined(ES_SPI1_MISO_GPIO_PORT)&&defined(ES_SPI1_MISO_GPIO_PIN)
  295. gpio_instruct.func = ES_SPI1_MISO_GPIO_FUNC;
  296. ald_gpio_init(ES_SPI1_MISO_GPIO_PORT, ES_SPI1_MISO_GPIO_PIN, &gpio_instruct);
  297. #endif
  298. spi_bus->parent.user_data = spi;
  299. result = rt_spi_bus_register(spi_bus, ES_DEVICE_NAME_SPI1_BUS, &es32f0_spi_ops);
  300. if (result != RT_EOK)
  301. {
  302. return result;
  303. }
  304. result = es32f0_spi_device_attach(ES_SPI1_NSS_PIN, ES_DEVICE_NAME_SPI1_BUS, ES_DEVICE_NAME_SPI1_DEV0);
  305. if (result != RT_EOK)
  306. {
  307. return result;
  308. }
  309. #endif
  310. return result;
  311. }
  312. INIT_BOARD_EXPORT(rt_hw_spi_init);
  313. #endif