forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi.h
261 lines (244 loc) · 7.05 KB
/
spi.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* @file
* @brief SPI Devicetree macro public API header file.
*/
/*
* Copyright (c) 2020 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DEVICETREE_SPI_H_
#define ZEPHYR_INCLUDE_DEVICETREE_SPI_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup devicetree-spi Devicetree SPI API
* @ingroup devicetree
* @{
*/
/**
* @brief Does a SPI controller node have chip select GPIOs configured?
*
* SPI bus controllers use the "cs-gpios" property for configuring
* chip select GPIOs. Its value is a phandle-array which specifies the
* chip select lines.
*
* Example devicetree fragment:
*
* spi1: spi@... {
* compatible = "vnd,spi";
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
* <&gpio2 20 GPIO_ACTIVE_LOW>;
* };
*
* spi2: spi@... {
* compatible = "vnd,spi";
* };
*
* Example usage:
*
* DT_SPI_HAS_CS_GPIOS(DT_NODELABEL(spi1)) // 1
* DT_SPI_HAS_CS_GPIOS(DT_NODELABEL(spi2)) // 0
*
* @param spi a SPI bus controller node identifier
* @return 1 if "spi" has a cs-gpios property, 0 otherwise
*/
#define DT_SPI_HAS_CS_GPIOS(spi) DT_NODE_HAS_PROP(spi, cs_gpios)
/**
* @brief Number of chip select GPIOs in a SPI controller's cs-gpios property
*
* Example devicetree fragment:
*
* spi1: spi@... {
* compatible = "vnd,spi";
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
* <&gpio2 20 GPIO_ACTIVE_LOW>;
* };
*
* spi2: spi@... {
* compatible = "vnd,spi";
* };
*
* Example usage:
*
* DT_SPI_NUM_CS_GPIOS(DT_NODELABEL(spi1)) // 2
* DT_SPI_NUM_CS_GPIOS(DT_NODELABEL(spi2)) // 0
*
* @param spi a SPI bus controller node identifier
* @return Logical length of spi's cs-gpios property, or 0 if "spi" doesn't
* have a cs-gpios property
*/
#define DT_SPI_NUM_CS_GPIOS(spi) \
COND_CODE_1(DT_SPI_HAS_CS_GPIOS(spi), \
(DT_PROP_LEN(spi, cs_gpios)), (0))
/**
* @brief Does a SPI device have a chip select line configured?
* Example devicetree fragment:
*
* spi1: spi@... {
* compatible = "vnd,spi";
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
* <&gpio2 20 GPIO_ACTIVE_LOW>;
*
* a: spi-dev-a@0 {
* reg = <0>;
* };
*
* b: spi-dev-b@1 {
* reg = <1>;
* };
* };
*
* spi2: spi@... {
* compatible = "vnd,spi";
* c: spi-dev-c@0 {
* reg = <0>;
* };
* };
*
* Example usage:
*
* DT_SPI_DEV_HAS_CS_GPIOS(DT_NODELABEL(a)) // 1
* DT_SPI_DEV_HAS_CS_GPIOS(DT_NODELABEL(b)) // 1
* DT_SPI_DEV_HAS_CS_GPIOS(DT_NODELABEL(c)) // 0
*
* @param spi_dev a SPI device node identifier
* @return 1 if spi_dev's bus node DT_BUS(spi_dev) has a chip select
* pin at index DT_REG_ADDR(spi_dev), 0 otherwise
*/
#define DT_SPI_DEV_HAS_CS_GPIOS(spi_dev) DT_SPI_HAS_CS_GPIOS(DT_BUS(spi_dev))
/**
* @brief Get a SPI device's chip select GPIO controller's node identifier
*
* Example devicetree fragment:
*
* gpio1: gpio@... { ... };
*
* gpio2: gpio@... { ... };
*
* spi@... {
* compatible = "vnd,spi";
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
* <&gpio2 20 GPIO_ACTIVE_LOW>;
*
* a: spi-dev-a@0 {
* reg = <0>;
* };
*
* b: spi-dev-b@1 {
* reg = <1>;
* };
* };
*
* Example usage:
*
* DT_SPI_DEV_CS_GPIOS_CTLR(DT_NODELABEL(a)) // DT_NODELABEL(gpio1)
* DT_SPI_DEV_CS_GPIOS_CTLR(DT_NODELABEL(b)) // DT_NODELABEL(gpio2)
*
* @param spi_dev a SPI device node identifier
* @return node identifier for spi_dev's chip select GPIO controller
*/
#define DT_SPI_DEV_CS_GPIOS_CTLR(spi_dev) \
DT_GPIO_CTLR_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
/**
* @brief Get a SPI device's chip select GPIO pin number
*
* It's an error if the GPIO specifier for spi_dev's entry in its
* bus node's cs-gpios property has no pin cell.
*
* Example devicetree fragment:
*
* spi1: spi@... {
* compatible = "vnd,spi";
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
* <&gpio2 20 GPIO_ACTIVE_LOW>;
*
* a: spi-dev-a@0 {
* reg = <0>;
* };
*
* b: spi-dev-b@1 {
* reg = <1>;
* };
* };
*
* Example usage:
*
* DT_SPI_DEV_CS_GPIOS_PIN(DT_NODELABEL(a)) // 10
* DT_SPI_DEV_CS_GPIOS_PIN(DT_NODELABEL(b)) // 20
*
* @param spi_dev a SPI device node identifier
* @return pin number of spi_dev's chip select GPIO
*/
#define DT_SPI_DEV_CS_GPIOS_PIN(spi_dev) \
DT_GPIO_PIN_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
/**
* @brief Get a SPI device's chip select GPIO flags
*
* Example devicetree fragment:
*
* spi1: spi@... {
* compatible = "vnd,spi";
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
*
* a: spi-dev-a@0 {
* reg = <0>;
* };
* };
*
* Example usage:
*
* DT_SPI_DEV_CS_GPIOS_FLAGS(DT_NODELABEL(a)) // GPIO_ACTIVE_LOW
*
* If the GPIO specifier for spi_dev's entry in its bus node's
* cs-gpios property has no flags cell, this expands to zero.
*
* @param spi_dev a SPI device node identifier
* @return flags value of spi_dev's chip select GPIO specifier, or
* zero if there is none
*/
#define DT_SPI_DEV_CS_GPIOS_FLAGS(spi_dev) \
DT_GPIO_FLAGS_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
/**
* @brief Equivalent to DT_SPI_DEV_HAS_CS_GPIOS(DT_DRV_INST(inst)).
* @param inst DT_DRV_COMPAT instance number
* @return 1 if the instance's bus has a CS pin at index
* DT_INST_REG_ADDR(inst), 0 otherwise
* @see DT_SPI_DEV_HAS_CS_GPIOS()
*/
#define DT_INST_SPI_DEV_HAS_CS_GPIOS(inst) \
DT_SPI_DEV_HAS_CS_GPIOS(DT_DRV_INST(inst))
/**
* @brief Get GPIO controller node identifier for a SPI device instance
* This is equivalent to DT_SPI_DEV_CS_GPIOS_CTLR(DT_DRV_INST(inst)).
* @param inst DT_DRV_COMPAT instance number
* @return node identifier for instance's chip select GPIO controller
* @see DT_SPI_DEV_CS_GPIOS_CTLR()
*/
#define DT_INST_SPI_DEV_CS_GPIOS_CTLR(inst) \
DT_SPI_DEV_CS_GPIOS_CTLR(DT_DRV_INST(inst))
/**
* @brief Equivalent to DT_SPI_DEV_CS_GPIOS_PIN(DT_DRV_INST(inst)).
* @param inst DT_DRV_COMPAT instance number
* @return pin number of the instance's chip select GPIO
* @see DT_SPI_DEV_CS_GPIOS_PIN()
*/
#define DT_INST_SPI_DEV_CS_GPIOS_PIN(inst) \
DT_SPI_DEV_CS_GPIOS_PIN(DT_DRV_INST(inst))
/**
* @brief DT_SPI_DEV_CS_GPIOS_FLAGS(DT_DRV_INST(inst)).
* @param inst DT_DRV_COMPAT instance number
* @return flags value of the instance's chip select GPIO specifier,
* or zero if there is none
* @see DT_SPI_DEV_CS_GPIOS_FLAGS()
*/
#define DT_INST_SPI_DEV_CS_GPIOS_FLAGS(inst) \
DT_SPI_DEV_CS_GPIOS_FLAGS(DT_DRV_INST(inst))
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_DEVICETREE_SPI_H_ */