drv_spi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-22 Sherman first version
  9. * 2020-11-02 xckhmf fixed bug
  10. */
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include "board.h"
  14. #include "drv_spi.h"
  15. #define DBG_LEVEL DBG_LOG
  16. #include <rtdbg.h>
  17. #define LOG_TAG "drv.spi"
  18. #ifdef BSP_USING_SPI
  19. #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
  20. static struct nrfx_drv_spi_config spi_config[] =
  21. {
  22. #ifdef BSP_USING_SPI0
  23. NRFX_SPI0_CONFIG,
  24. #endif
  25. #ifdef BSP_USING_SPI1
  26. NRFX_SPI1_CONFIG,
  27. #endif
  28. #ifdef BSP_USING_SPI2
  29. NRFX_SPI2_CONFIG,
  30. #endif
  31. };
  32. static struct nrfx_drv_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])];
  33. //Configure SPI bus pins using the menuconfig
  34. static struct nrfx_drv_spi_pin_config bsp_spi_pin[] =
  35. {
  36. #ifdef BSP_USING_SPI0
  37. {
  38. .sck_pin = BSP_SPI0_SCK_PIN,
  39. .mosi_pin = BSP_SPI0_MOSI_PIN,
  40. .miso_pin = BSP_SPI0_MISO_PIN,
  41. .ss_pin = BSP_SPI0_SS_PIN
  42. },
  43. #endif
  44. #ifdef BSP_USING_SPI1
  45. {
  46. .sck_pin = BSP_SPI1_SCK_PIN,
  47. .mosi_pin = BSP_SPI1_MOSI_PIN,
  48. .miso_pin = BSP_SPI1_MISO_PIN,
  49. .ss_pin = BSP_SPI1_SS_PIN
  50. },
  51. #endif
  52. #ifdef BSP_USING_SPI2
  53. {
  54. .sck_pin = BSP_SPI2_SCK_PIN,
  55. .mosi_pin = BSP_SPI2_MOSI_PIN,
  56. .miso_pin = BSP_SPI2_MISO_PIN,
  57. .ss_pin = BSP_SPI2_SS_PIN
  58. },
  59. #endif
  60. };
  61. static rt_uint8_t spi_index_find(struct rt_spi_bus *spi_bus)
  62. {
  63. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  64. {
  65. if(spi_bus == &spi_bus_obj[i].spi_bus)
  66. return i;
  67. }
  68. return 0xFF;
  69. }
  70. /**
  71. * spi event handler function
  72. */
  73. static void spi0_handler(const nrfx_spi_evt_t *p_event, void *p_context)
  74. {
  75. LOG_I("\nspi0_handler");
  76. }
  77. static void spi1_handler(const nrfx_spi_evt_t *p_event, void *p_context)
  78. {
  79. LOG_I("\nspi1_handler");
  80. }
  81. static void spi2_handler(const nrfx_spi_evt_t *p_event, void *p_context)
  82. {
  83. LOG_I("\nspi2_handler");
  84. }
  85. nrfx_spi_evt_handler_t spi_handler[] = {spi0_handler, spi1_handler, spi2_handler};
  86. /**
  87. * @brief This function config spi bus
  88. * @param device
  89. * @param configuration
  90. * @retval RT_EOK / RT_ERROR
  91. */
  92. static rt_err_t spi_configure(struct rt_spi_device *device,
  93. struct rt_spi_configuration *configuration)
  94. {
  95. RT_ASSERT(device != RT_NULL);
  96. RT_ASSERT(device->bus != RT_NULL);
  97. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  98. RT_ASSERT(configuration != RT_NULL);
  99. rt_uint8_t index = spi_index_find(device->bus);
  100. RT_ASSERT(index != 0xFF);
  101. nrfx_spi_t spi = spi_bus_obj[index].spi;
  102. nrfx_spi_config_t config = NRFX_SPI_DEFAULT_CONFIG(bsp_spi_pin[index].sck_pin,
  103. bsp_spi_pin[index].mosi_pin, bsp_spi_pin[index].miso_pin, NRFX_SPI_PIN_NOT_USED);
  104. /* spi config ss pin */
  105. if(device->parent.user_data != RT_NULL)
  106. {
  107. nrf_gpio_cfg_output((uint32_t)device->parent.user_data);
  108. }
  109. /* spi config bit order */
  110. if(configuration->mode & RT_SPI_MSB)
  111. {
  112. config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
  113. }
  114. else
  115. {
  116. config.bit_order = NRF_SPI_BIT_ORDER_LSB_FIRST;
  117. }
  118. /* spi mode config */
  119. switch (configuration->mode & RT_SPI_MODE_3 )
  120. {
  121. case RT_SPI_MODE_0/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */:
  122. config.mode = NRF_SPI_MODE_0;
  123. break;
  124. case RT_SPI_MODE_1/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */:
  125. config.mode = NRF_SPI_MODE_1;
  126. break;
  127. case RT_SPI_MODE_2/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */:
  128. config.mode = NRF_SPI_MODE_2;
  129. break;
  130. case RT_SPI_MODE_3/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */:
  131. config.mode = NRF_SPI_MODE_3;
  132. break;
  133. default:
  134. LOG_E("spi_configure mode error %x\n",configuration->mode);
  135. return RT_ERROR;
  136. }
  137. /* spi frequency config */
  138. switch (configuration->max_hz / 1000)
  139. {
  140. case 125:
  141. config.frequency = NRF_SPI_FREQ_125K;
  142. break;
  143. case 250:
  144. config.frequency = NRF_SPI_FREQ_250K;
  145. break;
  146. case 500:
  147. config.frequency = NRF_SPI_FREQ_500K;
  148. break;
  149. case 1000:
  150. config.frequency = NRF_SPI_FREQ_1M;
  151. break;
  152. case 2000:
  153. config.frequency = NRF_SPI_FREQ_2M;
  154. break;
  155. case 4000:
  156. config.frequency = NRF_SPI_FREQ_4M;
  157. break;
  158. case 8000:
  159. config.frequency = NRF_SPI_FREQ_8M;
  160. break;
  161. default:
  162. LOG_E("spi_configure rate error %d\n",configuration->max_hz);
  163. break;
  164. }
  165. rt_memcpy((void*)&spi_bus_obj[index].spi_config, (void*)&config, sizeof(nrfx_spi_config_t));
  166. nrfx_spi_evt_handler_t handler = RT_NULL; //spi send callback handler ,default NULL
  167. void * context = RT_NULL;
  168. nrfx_err_t nrf_ret = nrfx_spi_init(&spi, &config, handler, context);
  169. if(NRFX_SUCCESS == nrf_ret)
  170. return RT_EOK;
  171. return RT_ERROR;
  172. }
  173. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  174. {
  175. RT_ASSERT(device != RT_NULL);
  176. RT_ASSERT(device->bus != RT_NULL);
  177. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  178. rt_uint8_t index = spi_index_find(device->bus);
  179. nrfx_err_t nrf_ret;
  180. RT_ASSERT(index != 0xFF);
  181. nrfx_spi_t * p_instance = &spi_bus_obj[index].spi;
  182. nrfx_spi_xfer_desc_t p_xfer_desc;
  183. if(message->cs_take == 1)
  184. {
  185. nrf_gpio_pin_clear((uint32_t)device->parent.user_data);
  186. }
  187. p_xfer_desc.p_rx_buffer = message->recv_buf;
  188. p_xfer_desc.rx_length = message->length;
  189. p_xfer_desc.p_tx_buffer = message->send_buf;
  190. p_xfer_desc.tx_length = message->length ;
  191. if(message->send_buf == RT_NULL)
  192. {
  193. p_xfer_desc.tx_length = 0;
  194. }
  195. if(message->recv_buf == RT_NULL)
  196. {
  197. p_xfer_desc.rx_length = 0;
  198. }
  199. nrf_ret = nrfx_spi_xfer(p_instance, &p_xfer_desc, 0);
  200. if(message->cs_release == 1)
  201. {
  202. nrf_gpio_pin_set((uint32_t)device->parent.user_data);
  203. }
  204. if( NRFX_SUCCESS != nrf_ret)
  205. {
  206. return 0;
  207. }
  208. else
  209. {
  210. return message->length;
  211. }
  212. }
  213. /* spi bus callback function */
  214. static const struct rt_spi_ops nrfx_spi_ops =
  215. {
  216. .configure = spi_configure,
  217. .xfer = spixfer,
  218. };
  219. /*spi bus init*/
  220. static int rt_hw_spi_bus_init(void)
  221. {
  222. rt_err_t result = RT_ERROR;
  223. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  224. {
  225. spi_bus_obj[i].spi = spi_config[i].spi;
  226. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i]; //SPI INSTANCE
  227. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &nrfx_spi_ops);
  228. RT_ASSERT(result == RT_EOK);
  229. }
  230. return result;
  231. }
  232. int rt_hw_spi_init(void)
  233. {
  234. return rt_hw_spi_bus_init();
  235. }
  236. INIT_BOARD_EXPORT(rt_hw_spi_init);
  237. /**
  238. * Attach the spi device to SPI bus, this function must be used after initialization.
  239. */
  240. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin)
  241. {
  242. RT_ASSERT(bus_name != RT_NULL);
  243. RT_ASSERT(device_name != RT_NULL);
  244. RT_ASSERT(ss_pin != RT_NULL);
  245. rt_err_t result;
  246. struct rt_spi_device *spi_device;
  247. /* attach the device to spi bus*/
  248. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  249. RT_ASSERT(spi_device != RT_NULL);
  250. /* initialize the cs pin */
  251. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void*)ss_pin);
  252. if (result != RT_EOK)
  253. {
  254. LOG_E("%s attach to %s faild, %d", device_name, bus_name, result);
  255. result = RT_ERROR;
  256. }
  257. RT_ASSERT(result == RT_EOK);
  258. return result;
  259. }
  260. #endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
  261. #endif /*BSP_USING_SPI*/