•All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
spi.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014-2016 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
67 #ifndef PERIPH_SPI_H
68 #define PERIPH_SPI_H
69 
70 #include <endian.h>
71 #include <errno.h>
72 #include <stdbool.h>
73 #include <stddef.h>
74 #include <stdint.h>
75 
76 #include "periph_cpu.h"
77 #include "periph_conf.h"
78 #include "periph/gpio.h"
79 
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83 
88 #ifndef CONFIG_SPI_DMA_THRESHOLD_BYTES
89 #define CONFIG_SPI_DMA_THRESHOLD_BYTES 16
90 #endif
91 
95 #ifndef SPI_DEV
96 #define SPI_DEV(x) (x)
97 #endif
98 
102 #ifndef SPI_UNDEF
103 #define SPI_UNDEF (UINT_FAST8_MAX)
104 #endif
105 
109 #ifndef SPI_CS_UNDEF
110 #define SPI_CS_UNDEF (GPIO_UNDEF)
111 #endif
112 
120 #ifndef SPI_HWCS
121 #define SPI_HWCS(x) (SPI_CS_UNDEF)
122 #endif
123 
127 #ifndef HAVE_SPI_T
128 typedef uint_fast8_t spi_t;
129 #endif
130 
135 #ifndef HAVE_SPI_CS_T
136 typedef gpio_t spi_cs_t;
137 #endif
138 
145 enum {
146  SPI_OK = 0,
150  SPI_NOCLK = -EINVAL
151 };
152 
169 #ifndef HAVE_SPI_MODE_T
170 typedef enum {
174  SPI_MODE_3
176 #endif
177 
185 #ifndef HAVE_SPI_CLK_T
186 typedef enum {
193 #endif
194 
217 void spi_init(spi_t bus);
218 
236 void spi_init_pins(spi_t bus);
237 
257 int spi_init_cs(spi_t bus, spi_cs_t cs);
258 
259 #if defined(MODULE_PERIPH_SPI_RECONFIGURE) || DOXYGEN
260 
281 void spi_deinit_pins(spi_t dev);
282 
283 #if DOXYGEN
284 
295 gpio_t spi_pin_miso(spi_t dev);
296 
307 gpio_t spi_pin_mosi(spi_t dev);
308 
319 gpio_t spi_pin_clk(spi_t dev);
320 
321 #endif /* DOXYGEN */
322 #endif /* MODULE_PERIPH_SPI_RECONFIGURE */
323 
324 #if defined(MODULE_PERIPH_SPI_GPIO_MODE) || DOXYGEN
325 
329 typedef struct {
334 
345 int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t* mode);
346 #endif
347 
365 void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk);
366 
375 void spi_release(spi_t bus);
376 
388 uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out);
389 
401 void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
402  const void *out, void *in, size_t len);
403 
418 uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out);
419 
434 void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg,
435  const void *out, void *in, size_t len);
436 
448 static inline uint16_t spi_transfer_u16_be(spi_t bus, spi_cs_t cs, bool cont, uint16_t host_number)
449 {
450  const uint16_t send = htobe16(host_number);
451  uint16_t receive;
452  spi_transfer_bytes(bus, cs, cont, &send, &receive, sizeof(receive));
453  return be16toh(receive);
454 }
455 
456 #ifdef __cplusplus
457 }
458 #endif
459 
460 #endif /* PERIPH_SPI_H */
spi_clk_t
Definition: periph_cpu.h:352
Low-level GPIO peripheral driver interface definitions.
libc header for endian conversion
#define ENXIO
No such device or address.
Definition: errno.h:132
#define EINVAL
Invalid argument.
Definition: errno.h:97
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
uint_fast8_t spi_t
Default type for SPI devices.
Definition: spi.h:128
int spi_init_cs(spi_t bus, spi_cs_t cs)
Initialize the given chip select pin.
gpio_t spi_pin_miso(spi_t dev)
Get the MISO pin of the given SPI bus.
uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out)
Transfer one byte to/from a given register address.
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
Start a new SPI transaction.
gpio_t spi_pin_mosi(spi_t dev)
Get the MOSI pin of the given SPI bus.
int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t *mode)
Initialize MOSI/MISO/SCLK pins with adapted GPIO modes.
gpio_t spi_pin_clk(spi_t dev)
Get the CLK pin of the given SPI bus.
void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg, const void *out, void *in, size_t len)
Transfer a number of bytes to/from a given register address.
uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out)
Transfer one byte on the given SPI bus.
static uint16_t spi_transfer_u16_be(spi_t bus, spi_cs_t cs, bool cont, uint16_t host_number)
Transfer a 16 bit number in big endian byte order.
Definition: spi.h:448
void spi_release(spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
void spi_init_pins(spi_t bus)
Initialize the used SPI bus pins, i.e.
void spi_deinit_pins(spi_t dev)
Change the pins of the given SPI bus back to plain GPIO functionality.
spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: spi.h:170
void spi_init(spi_t bus)
Basic initialization of the given SPI bus.
void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont, const void *out, void *in, size_t len)
Transfer a number bytes using the given SPI bus.
spi_clk_t
Available SPI clock speeds.
Definition: spi.h:186
gpio_t spi_cs_t
Chip select pin type overlaps with gpio_t so it can be casted to this.
Definition: spi.h:136
@ SPI_OK
everything went as planned
Definition: spi.h:146
@ SPI_NOCS
invalid chip select line specified
Definition: spi.h:148
@ SPI_NOCLK
selected clock value is not supported
Definition: spi.h:150
@ SPI_NODEV
invalid SPI bus specified
Definition: spi.h:147
@ SPI_NOMODE
selected mode is not supported
Definition: spi.h:149
@ SPI_MODE_0
CPOL=0, CPHA=0.
Definition: spi.h:171
@ SPI_MODE_2
CPOL=1, CPHA=0.
Definition: spi.h:173
@ SPI_MODE_1
CPOL=0, CPHA=1.
Definition: spi.h:172
@ SPI_MODE_3
CPOL=1, CPHA=1.
Definition: spi.h:174
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: spi.h:191
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: spi.h:190
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: spi.h:188
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: spi.h:189
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: spi.h:187
static ssize_t send(int socket, const void *buffer, size_t length, int flags)
Send a message on a socket.
Definition: socket.h:456
uint16_t be16toh(uint16_t big_endian_16bits)
big endian to host, 16 bit
uint16_t htobe16(uint16_t host_16bits)
host to big endian, 16 bit
spi_mode_t
Support SPI modes.
Definition: periph_cpu.h:43
SPI gpio mode.
Definition: spi.h:329
gpio_mode_t mosi
GPIO mode used for MOSI pin.
Definition: spi.h:330
gpio_mode_t miso
GPIO mode used for MISO pin.
Definition: spi.h:331
gpio_mode_t sclk
GPIO mode used for SCLK pin.
Definition: spi.h:332