drv_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-11-10 liqiaozhong first commit
  11. * 2023-03-08 liqiaozhong support 4 spis and qspi working together
  12. *
  13. */
  14. #include"rtconfig.h"
  15. #ifdef BSP_USING_SPI
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include "interrupt.h"
  19. #define LOG_TAG "spi_drv"
  20. #include "drv_log.h"
  21. #include <string.h>
  22. #if defined(TARGET_E2000)
  23. #include "fparameters.h"
  24. #endif
  25. #include "fcpu_info.h"
  26. #include "fkernel.h"
  27. #include "ftypes.h"
  28. #include <dfs_file.h>
  29. #include "fspim.h"
  30. #include "fspim_hw.h" /* include low-level header file for internal probe */
  31. #include "drv_spi.h"
  32. /************************** Constant Definitions *****************************/
  33. /**************************** Type Definitions *******************************/
  34. /************************** Variable Definitions *****************************/
  35. #ifdef RT_USING_SPIM0
  36. static struct drv_spi _RTSpim0;
  37. #endif
  38. #ifdef RT_USING_SPIM1
  39. static struct drv_spi _RTSpim1;
  40. #endif
  41. #ifdef RT_USING_SPIM2
  42. static struct drv_spi _RTSpim2;
  43. #endif
  44. #ifdef RT_USING_SPIM3
  45. static struct drv_spi _RTSpim3;
  46. #endif
  47. static struct rt_spi_device *spi_device = RT_NULL;
  48. static struct rt_event rx_done_event;
  49. /***************** Macros (Inline Functions) Definitions *********************/
  50. #define EVENT_RX_DONE (1 << 1)
  51. /*******************************Api Functions*********************************/
  52. static rt_err_t spim_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  53. static rt_uint32_t spim_xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  54. static FError FSpimSetupInterrupt(FSpim *instance_p)
  55. {
  56. FASSERT(instance_p);
  57. FSpimConfig *config_p = &instance_p->config;
  58. uintptr base_addr = config_p->base_addr;
  59. u32 cpu_id = 0;
  60. GetCpuId(&cpu_id);
  61. LOG_D("cpu_id is %d, irq_num is %d\n", cpu_id, config_p->irq_num);
  62. config_p->irq_prority = 0xd0;
  63. rt_hw_interrupt_set_target_cpus(config_p->irq_num, cpu_id);
  64. rt_hw_interrupt_set_priority(config_p->irq_num, config_p->irq_prority);
  65. /* register intr callback */
  66. rt_hw_interrupt_install(config_p->irq_num,
  67. FSpimInterruptHandler,
  68. instance_p,
  69. NULL);
  70. /* enable tx fifo overflow / rx overflow / rx full */
  71. FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS);
  72. /* enable irq */
  73. rt_hw_interrupt_umask(config_p->irq_num);
  74. return FSPIM_SUCCESS;
  75. }
  76. static void rt_ft_send_event_done(void *instance_p, void *param)
  77. {
  78. FASSERT(instance_p);
  79. rt_event_send(&rx_done_event, EVENT_RX_DONE);
  80. return;
  81. }
  82. static const struct rt_spi_ops spim_ops =
  83. {
  84. .configure = spim_configure,
  85. .xfer = spim_xfer
  86. };
  87. static rt_err_t spim_configure(struct rt_spi_device *device,
  88. struct rt_spi_configuration *configuration)
  89. {
  90. FError ret = FSPIM_SUCCESS;
  91. RT_ASSERT(device != RT_NULL);
  92. RT_ASSERT(configuration != RT_NULL);
  93. struct drv_spi *user_data_cfg = device->parent.user_data;
  94. FSpimConfig input_cfg = *FSpimLookupConfig(user_data_cfg->spi_id);
  95. #ifdef RT_USING_SMART
  96. input_cfg.base_addr = (uintptr)rt_ioremap((void *)input_cfg.base_addr, 0x1000);
  97. #endif
  98. FSpimConfig *set_input_cfg = &input_cfg;
  99. /* set fspim device according to configuration */
  100. if (configuration->mode & RT_SPI_CPOL)
  101. {
  102. set_input_cfg->cpol = FSPIM_CPOL_HIGH;
  103. }
  104. else
  105. {
  106. set_input_cfg->cpol = FSPIM_CPOL_LOW;
  107. }
  108. if (configuration->mode & RT_SPI_CPHA)
  109. {
  110. set_input_cfg->cpha = FSPIM_CPHA_2_EDGE;
  111. }
  112. else
  113. {
  114. set_input_cfg->cpha = FSPIM_CPHA_1_EDGE;
  115. }
  116. if (configuration->data_width == 8)
  117. {
  118. set_input_cfg->n_bytes = FSPIM_1_BYTE;
  119. }
  120. else if (configuration->data_width == 16)
  121. {
  122. set_input_cfg->n_bytes = FSPIM_2_BYTE;
  123. }
  124. /* send spi_cfg to RT-Thread sys */
  125. ret = FSpimCfgInitialize(&user_data_cfg->spim_instance, &input_cfg);
  126. if (FSPIM_SUCCESS != ret)
  127. {
  128. return RT_ERROR;
  129. }
  130. /* irq setting */
  131. ret = FSpimSetupInterrupt(&user_data_cfg->spim_instance);
  132. if (FSPIM_SUCCESS != ret)
  133. {
  134. return RT_ERROR;
  135. }
  136. FSpimRegisterIntrruptHandler(&user_data_cfg->spim_instance, FSPIM_INTR_EVT_RX_DONE, rt_ft_send_event_done, NULL);
  137. return ret;
  138. }
  139. static rt_uint32_t spim_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  140. {
  141. RT_ASSERT(device != RT_NULL);
  142. RT_ASSERT(device->parent.user_data != RT_NULL);
  143. RT_ASSERT(message != RT_NULL);
  144. rt_size_t message_length;
  145. rt_uint8_t *recv_buf;
  146. const rt_uint8_t *send_buf;
  147. /* recv spi_cfg from RT-Thread sys */
  148. struct drv_spi *user_data_xfer = device->parent.user_data;
  149. FSpim *xfer_spim_instance = &user_data_xfer->spim_instance;
  150. FError tx_rx_result = FSPIM_SUCCESS;
  151. message_length = message->length;
  152. recv_buf = message->recv_buf;
  153. send_buf = message->send_buf;
  154. if (message->cs_take)
  155. {
  156. FSpimSetChipSelection(xfer_spim_instance, TRUE);
  157. }
  158. if (message_length > 0)
  159. {
  160. if (send_buf == RT_NULL && recv_buf != RT_NULL)
  161. {
  162. /* receive message */
  163. tx_rx_result = FSpimTransferByInterrupt(xfer_spim_instance, RT_NULL, recv_buf, message_length);
  164. }
  165. else if (send_buf != RT_NULL && recv_buf == RT_NULL)
  166. {
  167. /* send message */
  168. tx_rx_result = FSpimTransferByInterrupt(xfer_spim_instance, send_buf, RT_NULL, message_length);
  169. }
  170. else if (send_buf != RT_NULL && recv_buf != RT_NULL)
  171. {
  172. /* not supported yet */
  173. rt_kprintf("Do not support the situation that send_buf and recv_buf both not equal to 0.");
  174. }
  175. }
  176. if (FSPIM_SUCCESS != tx_rx_result)
  177. {
  178. rt_kprintf("FSpimTransferByInterrupt() fail!!!");
  179. message_length = 0;
  180. }
  181. if (rt_event_recv(&rx_done_event, (EVENT_RX_DONE),
  182. (RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR),
  183. RT_WAITING_FOREVER, RT_NULL) != RT_EOK)
  184. {
  185. rt_kprintf("Wait rx timeout!!!\n");
  186. message_length = 0;
  187. }
  188. if (message->cs_release)
  189. {
  190. FSpimSetChipSelection(xfer_spim_instance, FALSE);
  191. }
  192. return message_length;
  193. }
  194. int ft_spi_init(void)
  195. {
  196. rt_err_t result;
  197. static struct rt_spi_bus spim_bus;
  198. /* event creat */
  199. if (RT_EOK != rt_event_init(&rx_done_event, "rx_done_event", RT_IPC_FLAG_FIFO))
  200. {
  201. rt_kprintf("Create event failed.\n");
  202. return RT_ERROR;
  203. }
  204. /* spi bus init */
  205. result = rt_spi_bus_register(&spim_bus, "spi0", &spim_ops);
  206. RT_ASSERT((struct rt_spi_device *)rt_device_find("spi0"));
  207. rt_kprintf("Spi bus spi0 init\n");
  208. /* spi device init and attach to bus */
  209. #ifdef RT_USING_SPIM0
  210. _RTSpim0.spi_id = FSPI0_ID;
  211. result = rt_spi_bus_attach_device(&_RTSpim0.device, "spi00", "spi0", &_RTSpim0);
  212. spi_device = (struct rt_spi_device *)rt_device_find("spi00");
  213. if (RT_NULL == spi_device)
  214. {
  215. rt_kprintf("Spi init failed -> can't find spi00 device!\n");
  216. return RT_ERROR;
  217. }
  218. rt_kprintf("Spi master device spi00 init.\n");
  219. #endif
  220. #ifdef RT_USING_SPIM1
  221. _RTSpim1.spi_id = FSPI1_ID;
  222. result = rt_spi_bus_attach_device(&_RTSpim1.device, "spi01", "spi0", &_RTSpim1);
  223. spi_device = (struct rt_spi_device *)rt_device_find("spi01");
  224. if (RT_NULL == spi_device)
  225. {
  226. rt_kprintf("Spi init failed -> can't find spi01 device!\n");
  227. return RT_ERROR;
  228. }
  229. rt_kprintf("Spi master device spi01 init.\n");
  230. #endif
  231. #ifdef RT_USING_SPIM2
  232. _RTSpim2.spi_id = FSPI2_ID;
  233. result = rt_spi_bus_attach_device(&_RTSpim2.device, "spi02", "spi0", &_RTSpim2);
  234. spi_device = (struct rt_spi_device *)rt_device_find("spi02");
  235. if (RT_NULL == spi_device)
  236. {
  237. rt_kprintf("Spi init failed -> can't find spi02 device!\n");
  238. return RT_ERROR;
  239. }
  240. rt_kprintf("Spi master device spi02 init.\n");
  241. #endif
  242. #ifdef RT_USING_SPIM3
  243. _RTSpim3.spi_id = FSPI3_ID;
  244. result = rt_spi_bus_attach_device(&_RTSpim3.device, "spi03", "spi0", &_RTSpim3);
  245. spi_device = (struct rt_spi_device *)rt_device_find("spi03");
  246. if (RT_NULL == spi_device)
  247. {
  248. rt_kprintf("Spi init failed -> can't find spi03 device!\n");
  249. return RT_ERROR;
  250. }
  251. rt_kprintf("Spi master device spi03 init.\n");
  252. #endif
  253. return result;
  254. }
  255. INIT_DEVICE_EXPORT(ft_spi_init);
  256. /* spi test example */
  257. static void fspim_test_sample(int argc, char *argv[])
  258. {
  259. rt_uint8_t send_to_flash_id = 0x9f; /* Flash cmd */
  260. rt_uint8_t recv_from_falsh_id1[5] = {0};
  261. rt_uint8_t recv_from_falsh_id2[5] = {0};
  262. /* find the spi device to get the device handle */
  263. spi_device = (struct rt_spi_device *)rt_device_find("spi02");
  264. if (!spi_device)
  265. {
  266. rt_kprintf("fspim_test_sample run failed! can't find spi02 device!\n");
  267. }
  268. else
  269. {
  270. static struct rt_spi_message msg1, msg2;
  271. msg1.send_buf = &send_to_flash_id;
  272. msg1.recv_buf = RT_NULL;
  273. msg1.length = 1;
  274. msg1.cs_take = 1;
  275. msg1.cs_release = 0;
  276. msg1.next = &msg2;
  277. msg2.send_buf = RT_NULL;
  278. msg2.recv_buf = recv_from_falsh_id2;
  279. msg2.length = 5;
  280. msg2.cs_take = 0;
  281. msg2.cs_release = 1;
  282. msg2.next = RT_NULL;
  283. /* send the command to read the ID using rt_spi_send_then_recv() */
  284. rt_spi_send_then_recv(spi_device, &send_to_flash_id, 1, recv_from_falsh_id1, 5);
  285. rt_kprintf("use rt_spi_send_then_recv() read flash ID is:0x%x 0x%x 0x%x 0x%x 0x%x\n", recv_from_falsh_id1[0], recv_from_falsh_id1[1], recv_from_falsh_id1[2], recv_from_falsh_id1[3], recv_from_falsh_id1[4]);
  286. /* send the command to read the ID using rt_spi_transfer_message() */
  287. rt_spi_transfer_message(spi_device, &msg1);
  288. rt_kprintf("use rt_spi_transfer_message() read flash ID is:0x%x 0x%x 0x%x 0x%x 0x%x\n", recv_from_falsh_id2[0], recv_from_falsh_id2[1], recv_from_falsh_id2[2], recv_from_falsh_id2[3], recv_from_falsh_id2[4]);
  289. }
  290. }
  291. MSH_CMD_EXPORT(fspim_test_sample, "fspim test sample");
  292. #endif