1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-07-19     Rbbb666      first version
9  * 2024-03-30     xhackerustc  2nd version for FRDM-MCXN947
10  */
11 
12 #include "board.h"
13 
14 #include <drv_spi.h>
15 
16 #define SPI_NAME     "spi60"
17 #define CS_PIN       (3*32+23)
18 static struct rt_spi_device *spi_dev;
19 
20 /* attach spi device */
rt_spi_device_init(void)21 static int rt_spi_device_init(void)
22 {
23     struct rt_spi_configuration cfg;
24 
25     rt_hw_spi_device_attach("spi6", SPI_NAME, CS_PIN);
26 
27     cfg.data_width = 8;
28     cfg.mode   = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS;
29     cfg.max_hz = 1 *1000 *1000;
30 
31     spi_dev = (struct rt_spi_device *)rt_device_find(SPI_NAME);
32 
33     if (RT_NULL == spi_dev)
34     {
35         rt_kprintf("spi sample run failed! can't find %s device!\n", SPI_NAME);
36         return -RT_ERROR;
37     }
38 
39     rt_spi_configure(spi_dev, &cfg);
40 
41     return RT_EOK;
42 }
43 INIT_APP_EXPORT(rt_spi_device_init);
44 
45 /* spi loopback mode test case */
spi_sample(int argc,char ** argv)46 static int spi_sample(int argc, char **argv)
47 {
48     rt_uint8_t t_buf[32], r_buf[32];
49     int i = 0;
50     static struct rt_spi_message msg1;
51 
52     for (i = 0; i < sizeof(t_buf); i++)
53     {
54         t_buf[i] = i;
55     }
56 
57     msg1.send_buf   = &t_buf;
58     msg1.recv_buf   = &r_buf;
59     msg1.length     = sizeof(t_buf);
60     msg1.cs_take    = 1;
61     msg1.cs_release = 1;
62     msg1.next       = RT_NULL;
63 
64     rt_spi_transfer_message(spi_dev, &msg1);
65 
66     rt_kprintf("spi rbuf : ");
67     for (i = 0; i < sizeof(r_buf); i++)
68     {
69         rt_kprintf("%x ", r_buf[i]);
70     }
71 
72     rt_kprintf("\nspi loopback mode test over!\n");
73 
74     return RT_EOK;
75 }
76 MSH_CMD_EXPORT(spi_sample, spi loopback test);
77