1 /*
2  * Copyright (c) 2006-2025, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date             Author          Notes
8  * 2020-05-19       Sherman         first version
9  * 2023-01-28       Andrew          add Nrf5340 support
10  */
11 
12 #include <rtthread.h>
13 #include <rtdevice.h>
14 #include <rthw.h>
15 
16 #ifndef __DRV_SPI_H_
17 #define __DRV_SPI_H_
18 
19 #ifdef BSP_USING_SPI
20 #if defined(SOC_NRF5340)
21 #include "nrfx_spim.h"
22 
23 /**
24  * @brief Attach the spi device to SPI bus, this function must be used after initialization.
25  * @param bus_name     spi bus name  "spi0"/"spi1"/"spi2"
26  * @param device_name  spi device name "spi0x"/"spi1x"/"spi2x"
27  * @param ss_pin       spi ss pin number
28  * @retval  -RT_ERROR / RT_EOK
29 */
30 rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin);
31 
32 /* SPI bus config */
33 #ifdef BSP_USING_SPI0
34 #define NRFX_SPI0_CONFIG         \
35 {                                \
36     .bus_name = "spi0",          \
37     .spi = NRFX_SPIM_INSTANCE(0)  \
38 }
39 #endif
40 #ifdef BSP_USING_SPI1
41 #define NRFX_SPI1_CONFIG         \
42 {                                \
43     .bus_name = "spi1",          \
44     .spi = NRFX_SPIM_INSTANCE(1)  \
45 }
46 #endif
47 
48 #ifdef BSP_USING_SPI2
49 #define NRFX_SPI2_CONFIG         \
50 {                                \
51     .bus_name = "spi2",          \
52     .spi = NRFX_SPIM_INSTANCE(2)  \
53 }
54 #endif
55 
56 #ifdef BSP_USING_SPI3
57 #define NRFX_SPI3_CONFIG         \
58 {                                \
59     .bus_name = "spi3",          \
60     .spi = NRFX_SPIM_INSTANCE(3)  \
61 }
62 #endif
63 #ifdef BSP_USING_SPI4
64 #define NRFX_SPI4_CONFIG         \
65 {                                \
66     .bus_name = "spi4",          \
67     .spi = NRFX_SPIM_INSTANCE(4)  \
68 }
69 #endif
70 
71 struct nrfx_drv_spi_config
72 {
73     char *bus_name;
74     nrfx_spim_t spi;
75 };
76 
77 struct nrfx_drv_spi
78 {
79     nrfx_spim_t spi;    /* nrfx spi driver instance. */
80     nrfx_spim_config_t   spi_config; /* nrfx spi config Configuration */
81     struct rt_spi_configuration *cfg;
82     struct rt_spi_bus spi_bus;
83 };
84 
85 struct nrfx_drv_spi_pin_config
86 {
87     rt_uint8_t sck_pin;
88     rt_uint8_t mosi_pin;
89     rt_uint8_t miso_pin;
90     rt_uint8_t ss_pin;
91 };
92 #else
93 #include "nrfx_spi.h"
94 
95 /**
96  * @brief Attach the spi device to SPI bus, this function must be used after initialization.
97  * @param bus_name     spi bus name  "spi0"/"spi1"/"spi2"
98  * @param device_name  spi device name "spi0x"/"spi1x"/"spi2x"
99  * @param ss_pin       spi ss pin number
100  * @retval  -RT_ERROR / RT_EOK
101 */
102 rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin);
103 
104 /* SPI bus config */
105 #ifdef BSP_USING_SPI0
106 #define NRFX_SPI0_CONFIG         \
107 {                                \
108     .bus_name = "spi0",          \
109     .spi = NRFX_SPI_INSTANCE(0)  \
110 }
111 #endif
112 #ifdef BSP_USING_SPI1
113 #define NRFX_SPI1_CONFIG         \
114 {                                \
115     .bus_name = "spi1",          \
116     .spi = NRFX_SPI_INSTANCE(1)  \
117 }
118 #endif
119 
120 #ifdef BSP_USING_SPI2
121 #define NRFX_SPI2_CONFIG         \
122 {                                \
123     .bus_name = "spi2",          \
124     .spi = NRFX_SPI_INSTANCE(2)  \
125 }
126 #endif
127 
128 struct nrfx_drv_spi_config
129 {
130     char *bus_name;
131     nrfx_spi_t spi;
132 };
133 
134 struct nrfx_drv_spi
135 {
136     nrfx_spi_t spi;    /* nrfx spi driver instance. */
137     nrfx_spi_config_t   spi_config; /* nrfx spi config Configuration */
138     struct rt_spi_configuration *cfg;
139     struct rt_spi_bus spi_bus;
140 };
141 
142 struct nrfx_drv_spi_pin_config
143 {
144     rt_uint8_t sck_pin;
145     rt_uint8_t mosi_pin;
146     rt_uint8_t miso_pin;
147     rt_uint8_t ss_pin;
148 };
149 #endif
150 #endif /* BSP_USING_SPI */
151 #endif  /*__DRV_SPI_H_*/
152 
153