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 
220 void spi_init_pins(spi_t bus);
221 
241 int spi_init_cs(spi_t bus, spi_cs_t cs);
242 
243 #if defined(MODULE_PERIPH_SPI_RECONFIGURE) || DOXYGEN
244 
265 void spi_deinit_pins(spi_t dev);
266 
267 #if DOXYGEN
268 
279 gpio_t spi_pin_miso(spi_t dev);
280 
291 gpio_t spi_pin_mosi(spi_t dev);
292 
303 gpio_t spi_pin_clk(spi_t dev);
304 
305 #endif /* DOXYGEN */
306 #endif /* MODULE_PERIPH_SPI_RECONFIGURE */
307 
308 #if defined(MODULE_PERIPH_SPI_GPIO_MODE) || DOXYGEN
309 
313 typedef struct {
318 
329 #endif
330 
352 int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk);
353 
362 void spi_release(spi_t bus);
363 
375 uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out);
376 
388 void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
389  const void *out, void *in, size_t len);
390 
405 uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out);
406 
421 void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg,
422  const void *out, void *in, size_t len);
423 
424 #ifdef __cplusplus
425 }
426 #endif
427 
428 #endif /* PERIPH_SPI_H */
429 
SPI_CLK_400KHZ
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: spi.h:175
spi_transfer_bytes
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_pin_mosi
gpio_t spi_pin_mosi(spi_t dev)
Get the MOSI pin of the given SPI bus.
SPI_MODE_3
@ SPI_MODE_3
CPOL=1, CPHA=1.
Definition: spi.h:161
SPI_CLK_5MHZ
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: spi.h:177
SPI_CLK_10MHZ
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: spi.h:178
SPI_MODE_0
@ SPI_MODE_0
CPOL=0, CPHA=0.
Definition: spi.h:158
SPI_NODEV
@ SPI_NODEV
invalid SPI bus specified
Definition: spi.h:134
SPI_CLK_1MHZ
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: spi.h:176
SPI_NOCS
@ SPI_NOCS
invalid chip select line specified
Definition: spi.h:135
spi_gpio_mode_t::miso
gpio_mode_t miso
GPIO mode used for MISO pin.
Definition: spi.h:315
spi_mode_t
spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: spi.h:157
spi_release
void spi_release(spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
spi_cs_t
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_NOMODE
@ SPI_NOMODE
selected mode is not supported
Definition: spi.h:136
spi_pin_clk
gpio_t spi_pin_clk(spi_t dev)
Get the CLK pin of the given SPI bus.
spi_clk_t
spi_clk_t
Available SPI clock speeds.
Definition: spi.h:173
spi_acquire
int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
Start a new SPI transaction.
spi_gpio_mode_t::mosi
gpio_mode_t mosi
GPIO mode used for MOSI pin.
Definition: spi.h:314
spi_init_with_gpio_mode
int spi_init_with_gpio_mode(spi_t bus, spi_gpio_mode_t mode)
Initialize MOSI/MISO/SCLK pins with adapted GPIO modes.
spi_transfer_regs
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.
SPI_CLK_100KHZ
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: spi.h:174
spi_init
void spi_init(spi_t bus)
Basic initialization of the given SPI bus.
spi_pin_miso
gpio_t spi_pin_miso(spi_t dev)
Get the MISO pin of the given SPI bus.
spi_init_cs
int spi_init_cs(spi_t bus, spi_cs_t cs)
Initialize the given chip select pin.
spi_gpio_mode_t
SPI gpio mode.
Definition: spi.h:313
spi_transfer_reg
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.
gpio.h
Low-level GPIO peripheral driver interface definitions.
spi_init_pins
void spi_init_pins(spi_t bus)
Initialize the used SPI bus pins, i.e.
gpio_mode_t
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:70
SPI_OK
@ SPI_OK
everything went as planned
Definition: spi.h:133
spi_gpio_mode_t::sclk
gpio_mode_t sclk
GPIO mode used for SCLK pin.
Definition: spi.h:316
spi_transfer_byte
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.
spi_deinit_pins
void spi_deinit_pins(spi_t dev)
Change the pins of the given SPI bus back to plain GPIO functionality.
SPI_MODE_1
@ SPI_MODE_1
CPOL=0, CPHA=1.
Definition: spi.h:159
spi_t
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:118
SPI_MODE_2
@ SPI_MODE_2
CPOL=1, CPHA=0.
Definition: spi.h:160
SPI_NOCLK
@ SPI_NOCLK
selected clock value is not supported
Definition: spi.h:137