drv_spi.c 7.3 KB

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