Contiki-NG
spi.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017, Yanzi Networks.
3  * Copyright (c) 2017, University of Bristol - http://www.bristol.ac.uk/
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 /**
33  * \addtogroup dev
34  * @{
35  *
36  * \defgroup spi-hal SPI Hardware Abstraction Layer
37  *
38  * The SPI HAL provides a set of common functions that can be used in a
39  * platform-independent fashion.
40  *
41  *
42  * @{
43  *
44  * \file
45  * Header file for the SPI HAL
46  */
47 /*---------------------------------------------------------------------------*/
48 #ifndef SPI_H_
49 #define SPI_H_
50 /*---------------------------------------------------------------------------*/
51 #include "contiki.h"
52 #include "gpio-hal.h"
53 
54 #include <stdint.h>
55 #include <stdbool.h>
56 /*---------------------------------------------------------------------------*/
57 /* Include Arch-Specific conf */
58 #ifdef SPI_HAL_CONF_ARCH_HDR_PATH
59 #include SPI_HAL_CONF_ARCH_HDR_PATH
60 #endif /* SPI_HAL_CONF_ARCH_HDR_PATH */
61 /*---------------------------------------------------------------------------*/
62 #ifdef SPI_CONF_CONTROLLER_COUNT
63 /**
64  * \brief Number of SPI module instances on a chip
65  */
66 #define SPI_CONTROLLER_COUNT SPI_CONF_CONTROLLER_COUNT
67 #else
68 #define SPI_CONTROLLER_COUNT 0
69 #endif
70 /*---------------------------------------------------------------------------*/
71 /* Convenience macros to enumerate SPI module instances on a chip */
72 #define SPI_CONTROLLER_SPI0 0
73 #define SPI_CONTROLLER_SPI1 1
74 /*---------------------------------------------------------------------------*/
75 /**
76  * \brief SPI return codes
77  *
78  * @{
79  */
80 typedef enum {
81  SPI_DEV_STATUS_OK, /* Everything OK */
82  SPI_DEV_STATUS_EINVAL, /* Erroneous input value */
83  SPI_DEV_STATUS_TRANSFER_ERR, /* Error during SPI transfer */
84  SPI_DEV_STATUS_BUS_LOCKED, /* SPI bus is already locked */
85  SPI_DEV_STATUS_BUS_NOT_OWNED, /* SPI bus is locked by someone else */
86  SPI_DEV_STATUS_CLOSED /* SPI bus has not opened properly */
87 } spi_status_t;
88 /** @} */
89 /*---------------------------------------------------------------------------*/
90 /**
91  * \brief SPI Device Configuration
92  *
93  * This is a structure to an architecture-independent SPI configuration.
94  *
95  * @{
96  */
97 
98 typedef struct spi_device {
99  gpio_hal_pin_t pin_spi_sck; /* SPI SCK pin */
100  gpio_hal_pin_t pin_spi_miso; /* SPI MISO pin */
101  gpio_hal_pin_t pin_spi_mosi; /* SPI MOSI pin */
102  gpio_hal_pin_t pin_spi_cs; /* SPI Chip Select pin */
103  uint32_t spi_bit_rate; /* SPI bit rate */
104  uint8_t spi_pha; /* SPI mode phase */
105  uint8_t spi_pol; /* SPI mode polarity */
106  uint8_t spi_controller; /* ID of SPI controller to use */
107 } spi_device_t;
108 /** @} */
109 /*---------------------------------------------------------------------------*/
110 /* These are architecture-independent functions to be used by SPI devices. */
111 /*---------------------------------------------------------------------------*/
112 /**
113  * \brief Locks and then opens an SPI controller
114  * \param dev An SPI device configuration which defines the controller
115  * to be locked and the opening configuration.
116  * \return SPI return code
117  */
119 
120 /**
121  * \brief Closes and then unlocks an SPI controller
122  * \param dev An SPI device configuration which defines the controller
123  * to be closed and unlocked.
124  * \return SPI return code
125  *
126  * Releasing an SPI controller should put it in low-power mode.
127  * This should work only if the device has already locked the SPI
128  * controller.
129  */
131 
132 /**
133  * \brief Selects the SPI peripheral
134  * \param dev An SPI device configuration which defines the CS pin.
135  * \return SPI return code
136  *
137  * Clears the CS pin. This should work only if the device has
138  * already locked the SPI controller.
139  */
141 
142 /**
143  * \brief Deselects the SPI peripheral
144  * \param dev An SPI device configuration which defines the CS pin.
145  * \return SPI return code
146  *
147  * Sets the CS pin. Lock is not required.
148  */
150 
151 /**
152  * \brief Checks if a device has locked an SPI controller
153  * \param dev An SPI device configuration which defines the controller.
154  * \return true if the device has the lock, false otherwise.
155  */
156 bool spi_has_bus(const spi_device_t *dev);
157 
158 /**
159  * \brief Writes a single byte to an SPI device
160  * \param dev An SPI device configuration.
161  * \param data A byte of data
162  * \return SPI return code
163  *
164  * It should work only if the device has already locked the SPI controller.
165  */
166 spi_status_t spi_write_byte(const spi_device_t *dev, uint8_t data);
167 
168 /**
169  * \brief Reads a single byte from an SPI device
170  * \param dev An SPI device configuration.
171  * \param data A pointer to a byte of data
172  * \return SPI return code
173  *
174  * It should work only if the device has already locked the SPI controller.
175  */
176 spi_status_t spi_read_byte(const spi_device_t *dev, uint8_t *data);
177 
178 /**
179  * \brief Writes a buffer to an SPI device
180  * \param dev An SPI device configuration.
181  * \param data A pointer to the data
182  * \param size Size of the data to write
183  * \return SPI return code
184  *
185  * It should work only if the device has already locked the SPI controller.
186  */
188  const uint8_t *data, int size);
189 
190 /**
191  * \brief Reads a buffer from an SPI device
192  * \param dev An SPI device configuration.
193  * \param data A pointer to the data
194  * \param size Size of the data to read
195  * \return SPI return code
196  *
197  * It should work only if the device has already locked the SPI controller.
198  */
199 spi_status_t spi_read(const spi_device_t *dev, uint8_t *data, int size);
200 
201 /**
202  * \brief Reads and ignores data from an SPI device
203  * \param dev An SPI device configuration.
204  * \param size Size of the data to read and ignore
205  * \return SPI return code
206  *
207  * Reads size bytes from the SPI and throws them away.
208  * It should work only if the device has already locked the SPI controller.
209  */
210 spi_status_t spi_read_skip(const spi_device_t *dev, int size);
211 
212 /**
213  * \brief Performs a generic SPI transfer
214  * \param dev An SPI device configuration.
215  * \param data A pointer to the data to be written. Set it to NULL to
216  * skip writing.
217  * \param wsize Size of data to write.
218  * \param buf A pointer to buffer to copy the data read. Set to NULL
219  * to skip reading.
220  * \param rsize Size of data to read.
221  * \param ignore Size of data to read and ignore.
222  * \return SPI return code
223  *
224  * It should work only if the device has already locked the SPI controller.
225  * A total of rlen+ignore_len bytes will be read. The first rlen bytes will
226  * be copied to buf. The remaining ignore_len bytes won't be copied to the
227  * buffer. The maximum of wlen and rlen+ignore_len of bytes will be transfered.
228  */
230  const uint8_t *data, int wsize,
231  uint8_t *buf, int rsize, int ignore);
232 
233 /**
234  * \brief Reads and Writes one byte from/to an SPI device
235  * \param dev An SPI device configuration.
236  * \param strobe Byte to write
237  * \param status Pointer to byte to read
238  * \return SPI return code
239  *
240  * It should work only if the device has already locked the SPI controller.
241  */
242 spi_status_t spi_strobe(const spi_device_t *dev, uint8_t strobe,
243  uint8_t *status);
244 
245 /**
246  * \brief Reads a buffer of bytes from a register of an SPI device
247  * \param dev An SPI device configuration.
248  * \param reg Register
249  * \param data A pointer to the data
250  * \param size Size of the data to read
251  * \return SPI return code
252  *
253  * It should work only if the device has already locked the SPI controller.
254  */
255 spi_status_t spi_read_register(const spi_device_t *dev, uint8_t reg,
256  uint8_t *data, int size);
257 
258 /*---------------------------------------------------------------------------*/
259 /* These are architecture-specific functions to be implemented by each CPU. */
260 /*---------------------------------------------------------------------------*/
261 
262 /**
263  * \brief Checks if a device has locked an SPI controller
264  * \param dev An SPI device configuration which defines the controller
265  * to be checked if it is locked and the respective device.
266  * \return 1 if the device has the lock, 0 otherwise.
267  *
268  */
269 bool spi_arch_has_lock(const spi_device_t *dev);
270 
271 /**
272  * \brief Checks if an SPI controller is locked by any device
273  * \param dev An SPI device configuration which defines the controller
274  * to be checked.
275  * \return 1 if the controller is locked, 0 otherwise.
276  *
277  */
278 bool spi_arch_is_bus_locked(const spi_device_t *dev);
279 
280 /**
281  * \brief Locks and opens an SPI controller to the configuration specified.
282  * \param dev An SPI device configuration.
283  * \return SPI return code
284  *
285  * This should work only if the device has already locked the SPI
286  * controller.
287  *
288  */
290 
291 /**
292  * \brief Closes and unlocks an SPI controller
293  * \param dev An SPI device configuration that specifies the controller.
294  * \return SPI return code
295  *
296  * This should turn off the SPI controller to put it in low power mode
297  * and unlock it.
298  * It should work only if the device has already locked the SPI
299  * controller.
300  *
301  */
303 
304 /**
305  * \brief Performs an SPI transfer
306  * \param dev An SPI device configuration that specifies the controller.
307  * \param data A pointer to the data to be written. Set it to NULL to
308  * skip writing.
309  * \param wlen Length of data to write.
310  * \param buf A pointer to buffer to copy the data read. Set to NULL
311  * to skip reading.
312  * \param rlen Length of data to read.
313  * \param ignore_len Length of data to read and ignore.
314  * \return SPI return code
315  *
316  * It should work only if the device has already locked the SPI controller.
317  * A total of rlen+ignore_len bytes will be read. The first rlen bytes will
318  * be copied to buf. The remaining ignore_len bytes won't be copied to the
319  * buffer. The maximum of wlen and rlen+ignore_len of bytes will be transfered.
320  */
322  const uint8_t *data, int wlen,
323  uint8_t *buf, int rlen,
324  int ignore_len);
325 
326 #endif /* SPI_H_ */
327 /*---------------------------------------------------------------------------*/
328 /**
329  * @}
330  * @}
331  */
bool spi_arch_has_lock(const spi_device_t *dev)
Checks if a device has locked an SPI controller.
Definition: spi-arch.c:151
struct spi_device spi_device_t
SPI Device Configuration.
spi_status_t spi_select(const spi_device_t *dev)
Selects the SPI peripheral.
Definition: spi.c:68
spi_status_t spi_read(const spi_device_t *dev, uint8_t *buf, int size)
Reads a buffer from an SPI device.
Definition: spi.c:140
spi_status_t spi_write_byte(const spi_device_t *dev, uint8_t data)
Writes a single byte to an SPI device.
Definition: spi.c:98
spi_status_t spi_arch_transfer(const spi_device_t *dev, const uint8_t *data, int wlen, uint8_t *buf, int rlen, int ignore_len)
Performs an SPI transfer.
Definition: spi-arch.c:286
spi_status_t spi_transfer(const spi_device_t *dev, const uint8_t *wdata, int wsize, uint8_t *rbuf, int rsize, int ignore)
Performs a generic SPI transfer.
Definition: spi.c:168
spi_status_t spi_read_skip(const spi_device_t *dev, int size)
Reads and ignores data from an SPI device.
Definition: spi.c:154
spi_status_t
SPI return codes.
Definition: spi.h:80
spi_status_t spi_arch_lock_and_open(const spi_device_t *dev)
Locks and opens an SPI controller to the configuration specified.
Definition: spi-arch.c:171
spi_status_t spi_strobe(const spi_device_t *dev, uint8_t strobe, uint8_t *result)
Reads and Writes one byte from/to an SPI device.
Definition: spi.c:215
spi_status_t spi_read_register(const spi_device_t *dev, uint8_t reg, uint8_t *data, int size)
Reads a buffer of bytes from a register of an SPI device.
Definition: spi.c:192
bool spi_has_bus(const spi_device_t *dev)
Checks if a device has locked an SPI controller.
Definition: spi.c:88
spi_status_t spi_read_byte(const spi_device_t *dev, uint8_t *buf)
Reads a single byte from an SPI device.
Definition: spi.c:126
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:77
spi_status_t spi_write(const spi_device_t *dev, const uint8_t *data, int size)
Writes a buffer to an SPI device.
Definition: spi.c:112
spi_status_t spi_acquire(const spi_device_t *dev)
Locks and then opens an SPI controller.
Definition: spi.c:46
spi_status_t spi_deselect(const spi_device_t *dev)
Deselects the SPI peripheral.
Definition: spi.c:80
bool spi_arch_is_bus_locked(const spi_device_t *dev)
Checks if an SPI controller is locked by any device.
Definition: spi-arch.c:161
SPI Device Configuration.
Definition: spi.h:98
spi_status_t spi_release(const spi_device_t *dev)
Closes and then unlocks an SPI controller.
Definition: spi.c:57
Header file for the GPIO HAL.
spi_status_t spi_arch_close_and_unlock(const spi_device_t *dev)
Closes and unlocks an SPI controller.
Definition: spi-arch.c:268