1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2020-09-15 thread-liu first version
9 */
10
11 #include "board.h"
12
13 #if defined(BSP_USING_SPI1)
14 #include <drv_spi.h>
15
16 #define SPI_NAME "spi1"
17 #define SPI_DEVICE_NAME "spi10"
18 static struct rt_spi_device *spi_dev = RT_NULL;
19
20 /* attach spi1 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(SPI_NAME, SPI_DEVICE_NAME, PIN_NONE);
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_DEVICE_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 /* spi5 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[8], r_buf[8];
49 int i = 0;
50 static struct rt_spi_message msg1;
51
52 if (argc != 9)
53 {
54 rt_kprintf("Usage:\n");
55 rt_kprintf("spi_sample 1 2 3 4 5 6 7 8\n");
56 return -RT_ERROR;
57 }
58
59 for (i = 0; i < 8; i++)
60 {
61 t_buf[i] = atoi(argv[i+1]);
62 }
63
64 msg1.send_buf = &t_buf;
65 msg1.recv_buf = &r_buf;
66 msg1.length = sizeof(t_buf);
67 msg1.cs_take = 1;
68 msg1.cs_release = 0;
69 msg1.next = RT_NULL;
70
71 rt_spi_transfer_message(spi_dev, &msg1);
72
73 rt_kprintf("spi rbuf : ");
74 for (i = 0; i < sizeof(t_buf); i++)
75 {
76 rt_kprintf("%x ", r_buf[i]);
77 }
78
79 rt_kprintf("\nspi loopback mode test over!\n");
80
81 return RT_EOK;
82 }
83 MSH_CMD_EXPORT(spi_sample, spi loopback test);
84
85 #endif /* BSP_USING_SPI5 */
86