1 #include"rtconfig.h"
2 #if defined(BSP_USING_SPI_LAYER)
3 #include"rtconfig.h"
4 #include <rtthread.h>
5 #include <rtdevice.h>
6 #include "auto_test.h"
7 #include "interrupt.h"
8 #if defined(BSP_USING_SPI)
9 #define LOG_TAG "spi_drv"
10 #elif defined(BSP_USING_SPI_MSG)
11 #define LOG_TAG "spi_msg_drv"
12 #endif
13 #include "drv_log.h"
14 #include <string.h>
15 #include "fparameters.h"
16 #include "fcpu_info.h"
17 #include "fkernel.h"
18 #include "ftypes.h"
19 #include <dfs_file.h>
20
21 #if defined(BSP_USING_SPI)
22 #include "fspim.h"
23 #include "fspim_hw.h" /* include low-level header file for internal probe */
24 #include "drv_spi.h"
25 #elif defined(BSP_USING_SPI_MSG)
26 #include "fspim_msg.h"
27 #include "fspim_msg_hw.h" /* include low-level header file for internal probe */
28 #include "drv_spi_msg.h"
29 #endif
30
31 static struct rt_spi_device spi_flash_device;
32 /* spi test example */
fspim_test_sample()33 rt_err_t fspim_test_sample()
34 {
35 static struct rt_spi_device *spi_device = RT_NULL;
36 static struct rt_spi_device *spi_bus = RT_NULL;
37 rt_err_t res = RT_EOK;
38
39 #if defined(E2000D_DEMO_BOARD)||defined(E2000Q_DEMO_BOARD)
40 spi_bus = (struct rt_spi_device *)rt_device_find("SPI2");
41 rt_spi_bus_attach_device(&spi_flash_device, "flash", "SPI2", spi_bus);
42 #endif
43
44 #if defined(FIREFLY_DEMO_BOARD)||defined(CUS_DEMO_BOARD)
45 spi_bus = (struct rt_spi_device *)rt_device_find("SPI0");
46 rt_spi_bus_attach_device(&spi_flash_device, "flash", "SPI0", spi_bus);
47 #endif
48
49 rt_uint8_t send_to_flash_id = 0x9f; /* Flash cmd */
50 rt_uint8_t recv_from_falsh_id1[5] = {0};
51 rt_uint8_t recv_from_falsh_id2[5] = {0};
52
53 /* find the spi device to get the device handle */
54 spi_device = (struct rt_spi_device *)rt_device_find("flash");
55 if (!spi_device)
56 {
57 rt_kprintf("fspim_test_sample run failed! can't find flash device!\n");
58 }
59 else
60 {
61 static struct rt_spi_message msg1, msg2;
62
63 msg1.send_buf = &send_to_flash_id;
64 msg1.recv_buf = RT_NULL;
65 msg1.length = 1;
66 msg1.cs_take = 1;
67 msg1.cs_release = 0;
68 msg1.next = &msg2;
69
70 msg2.send_buf = RT_NULL;
71 msg2.recv_buf = recv_from_falsh_id2;
72 msg2.length = 5;
73 msg2.cs_take = 0;
74 msg2.cs_release = 1;
75 msg2.next = RT_NULL;
76
77 /* send the command to read the ID using rt_spi_send_then_recv() */
78 rt_spi_send_then_recv(spi_device, &send_to_flash_id, 1, recv_from_falsh_id1, 5);
79 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]);
80
81 /* send the command to read the ID using rt_spi_transfer_message() */
82 rt_spi_transfer_message(spi_device, &msg1);
83 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]);
84
85 for (int i = 0; i < 5; i++)
86 {
87 if(recv_from_falsh_id1[i] == 0xff)
88 {
89 res = RT_ERROR;
90 goto exit;
91 }
92 }
93 for (int j = 0; j < 5; j++)
94 {
95 if(recv_from_falsh_id2[j] == 0xff)
96 {
97 res = RT_ERROR;
98 goto exit;
99 }
100 }
101 }
102 exit:
103 /* print message on example run result */
104 if (res == 0)
105 {
106 rt_kprintf("%s@%d:rtthread spi test example [success].\r\n", __func__, __LINE__);
107 }
108 else
109 {
110 rt_kprintf("%s@%d:rtthread spi test example [failure], res = %d\r\n", __func__, __LINE__, res);
111 }
112
113 return res;
114
115 }
116 MSH_CMD_EXPORT(fspim_test_sample, "fspim test sample");
117 #endif