summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/drivers_example/spi_sample.c
blob: 1827ed055a790018bc74945e22cf981fbfcb0da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include"rtconfig.h"
#if defined(BSP_USING_SPI_LAYER)
    #include"rtconfig.h"
    #include <rtthread.h>
    #include <rtdevice.h>
    #include "auto_test.h"
    #include "interrupt.h"
    #if defined(BSP_USING_SPI)
        #define LOG_TAG      "spi_drv"
    #elif defined(BSP_USING_SPI_MSG)
        #define LOG_TAG      "spi_msg_drv"
    #endif
    #include "drv_log.h"
    #include <string.h>
    #include "fparameters.h"
    #include "fcpu_info.h"
    #include "fkernel.h"
    #include "ftypes.h"
    #include <dfs_file.h>

    #if defined(BSP_USING_SPI)
        #include "fspim.h"
        #include "fspim_hw.h" /* include low-level header file for internal probe */
        #include "drv_spi.h"
    #elif defined(BSP_USING_SPI_MSG)
        #include "fspim_msg.h"
        #include "fspim_msg_hw.h" /* include low-level header file for internal probe */
        #include "drv_spi_msg.h"
    #endif

static struct rt_spi_device spi_flash_device;
/* spi test example */
rt_err_t fspim_test_sample()
{
    static struct rt_spi_device *spi_device = RT_NULL;
    static struct rt_spi_device *spi_bus = RT_NULL;
    rt_err_t res = RT_EOK;

#if defined(E2000D_DEMO_BOARD)||defined(E2000Q_DEMO_BOARD)
    spi_bus = (struct rt_spi_device *)rt_device_find("SPI2");
    rt_spi_bus_attach_device(&spi_flash_device, "flash", "SPI2", spi_bus);
#endif

#if defined(FIREFLY_DEMO_BOARD)||defined(CUS_DEMO_BOARD)
    spi_bus = (struct rt_spi_device *)rt_device_find("SPI0");
    rt_spi_bus_attach_device(&spi_flash_device, "flash", "SPI0", spi_bus);
#endif

    rt_uint8_t send_to_flash_id = 0x9f; /* Flash cmd */
    rt_uint8_t recv_from_falsh_id1[5] = {0};
    rt_uint8_t recv_from_falsh_id2[5] = {0};

    /* find the spi device to get the device handle */
    spi_device = (struct rt_spi_device *)rt_device_find("flash");
    if (!spi_device)
    {
        rt_kprintf("fspim_test_sample run failed! can't find flash device!\n");
    }
    else
    {
        static struct rt_spi_message msg1, msg2;

        msg1.send_buf   = &send_to_flash_id;
        msg1.recv_buf   = RT_NULL;
        msg1.length     = 1;
        msg1.cs_take    = 1;
        msg1.cs_release = 0;
        msg1.next       = &msg2;

        msg2.send_buf   = RT_NULL;
        msg2.recv_buf   = recv_from_falsh_id2;
        msg2.length     = 5;
        msg2.cs_take    = 0;
        msg2.cs_release = 1;
        msg2.next       = RT_NULL;

        /* send the command to read the ID using rt_spi_send_then_recv() */
        rt_spi_send_then_recv(spi_device, &send_to_flash_id, 1, recv_from_falsh_id1, 5);
        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]);

        /* send the command to read the ID using rt_spi_transfer_message() */
        rt_spi_transfer_message(spi_device, &msg1);
        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]);

        for (int i = 0; i < 5; i++)
        {
            if(recv_from_falsh_id1[i] == 0xff)
            {
                res = RT_ERROR;
                goto exit;
            }
        }
        for (int j = 0; j < 5; j++)
        {
            if(recv_from_falsh_id2[j] == 0xff)
            {
                res = RT_ERROR;
                goto exit;
            }
        }
    }
exit:
    /* print message on example run result */
    if (res == 0)
    {
        rt_kprintf("%s@%d:rtthread spi test example [success].\r\n", __func__, __LINE__);
    }
    else
    {
        rt_kprintf("%s@%d:rtthread spi test example [failure], res = %d\r\n", __func__, __LINE__, res);
    }

    return res;

}
MSH_CMD_EXPORT(fspim_test_sample, "fspim test sample");
#endif