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 
66 #ifndef PERIPH_SPI_H
67 #define PERIPH_SPI_H
68 
69 #include <stdbool.h>
70 #include <stddef.h>
71 #include <stdint.h>
72 #include <limits.h>
73 
74 #include "periph_cpu.h"
75 #include "periph_conf.h"
76 #include "periph/gpio.h"
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
85 #ifndef SPI_DEV
86 #define SPI_DEV(x) (x)
87 #endif
88 
92 #ifndef SPI_UNDEF
93 #define SPI_UNDEF (UINT_MAX)
94 #endif
95 
99 #ifndef SPI_CS_UNDEF
100 #define SPI_CS_UNDEF (GPIO_UNDEF)
101 #endif
102 
110 #ifndef SPI_HWCS
111 #define SPI_HWCS(x) (SPI_CS_UNDEF)
112 #endif
113 
117 #ifndef HAVE_SPI_T
118 typedef unsigned int spi_t;
119 #endif
120 
125 #ifndef HAVE_SPI_CS_T
126 typedef gpio_t spi_cs_t;
127 #endif
128 
132 enum {
133  SPI_OK = 0,
134  SPI_NODEV = -1,
135  SPI_NOCS = -2,
136  SPI_NOMODE = -3,
137  SPI_NOCLK = -4
138 };
139 
156 #ifndef HAVE_SPI_MODE_T
157 typedef enum {
162 } spi_mode_t;
163 #endif
164 
172 #ifndef HAVE_SPI_CLK_T
173 typedef enum {
179 } spi_clk_t;
180 #endif
181 
201 void spi_init(spi_t bus);
202 
218 void spi_init_pins(spi_t bus);
219 
239 int spi_init_cs(spi_t bus, spi_cs_t cs);
240 
241 #if defined(MODULE_PERIPH_SPI_GPIO_MODE) || DOXYGEN
242 
246 typedef struct {
251 
261 int spi_init_with_gpio_mode(spi_t bus, spi_gpio_mode_t mode);
262 #endif
263 
285 int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk);
286 
295 void spi_release(spi_t bus);
296 
308 uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out);
309 
321 void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
322  const void *out, void *in, size_t len);
323 
338 uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out);
339 
354 void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg,
355  const void *out, void *in, size_t len);
356 
357 #ifdef __cplusplus
358 }
359 #endif
360 
361 #endif /* PERIPH_SPI_H */
362 
CPOL=0, CPHA=1.
Definition: spi.h:159
gpio_mode_t sclk
GPIO mode used for SCLK pin.
Definition: spi.h:249
gpio_mode_t mosi
GPIO mode used for MOSI pin.
Definition: spi.h:247
Low-level GPIO peripheral driver interface definitions.
selected clock value is not supported
Definition: spi.h:137
CPOL=0, CPHA=0.
Definition: spi.h:158
drive the SPI bus with 100KHz
Definition: spi.h:174
drive the SPI bus with 400KHz
Definition: spi.h:175
void spi_release(spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
gpio_t spi_cs_t
Chip select pin type overlaps with gpio_t so it can be casted to this.
Definition: spi.h:126
spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: spi.h:157
int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
Start a new SPI transaction.
spi_clk_t
Available SPI clock speeds.
Definition: spi.h:173
CPOL=1, CPHA=1.
Definition: spi.h:161
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.
void spi_init(spi_t bus)
Basic initialization of the given SPI bus.
drive the SPI bus with 5MHz
Definition: spi.h:177
int spi_init_with_gpio_mode(spi_t bus, spi_gpio_mode_t mode)
Initialize MOSI/MISO/SCLK pins with adapted GPIO modes.
drive the SPI bus with 10MHz
Definition: spi.h:178
drive the SPI bus with 1MHz
Definition: spi.h:176
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.
everything went as planned
Definition: spi.h:133
SPI gpio mode.
Definition: spi.h:246
invalid SPI bus specified
Definition: spi.h:134
int spi_init_cs(spi_t bus, spi_cs_t cs)
Initialize the given chip select pin.
gpio_mode_t
Available pin modes.
Definition: gpio.h:113
void spi_init_pins(spi_t bus)
Initialize the used SPI bus pins, i.e.
CPOL=1, CPHA=0.
Definition: spi.h:160
invalid chip select line specified
Definition: spi.h:135
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.
gpio_mode_t miso
GPIO mode used for MISO pin.
Definition: spi.h:248
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.
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:118
selected mode is not supported
Definition: spi.h:136