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 
53 #ifndef PERIPH_SPI_H
54 #define PERIPH_SPI_H
55 
56 #include <stdbool.h>
57 #include <stddef.h>
58 #include <stdint.h>
59 #include <limits.h>
60 
61 #include "periph_cpu.h"
62 #include "periph_conf.h"
63 #include "periph/gpio.h"
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
72 #ifndef SPI_DEV
73 #define SPI_DEV(x) (x)
74 #endif
75 
79 #ifndef SPI_UNDEF
80 #define SPI_UNDEF (UINT_MAX)
81 #endif
82 
86 #ifndef SPI_CS_UNDEF
87 #define SPI_CS_UNDEF (GPIO_UNDEF)
88 #endif
89 
97 #ifndef SPI_HWCS
98 #define SPI_HWCS(x) (SPI_CS_UNDEF)
99 #endif
100 
104 #ifndef HAVE_SPI_T
105 typedef unsigned int spi_t;
106 #endif
107 
112 #ifndef HAVE_SPI_CS_T
113 typedef gpio_t spi_cs_t;
114 #endif
115 
119 enum {
120  SPI_OK = 0,
121  SPI_NODEV = -1,
122  SPI_NOCS = -2,
123  SPI_NOMODE = -3,
124  SPI_NOCLK = -4
125 };
126 
143 #ifndef HAVE_SPI_MODE_T
144 typedef enum {
149 } spi_mode_t;
150 #endif
151 
159 #ifndef HAVE_SPI_CLK_T
160 typedef enum {
166 } spi_clk_t;
167 #endif
168 
188 void spi_init(spi_t bus);
189 
205 void spi_init_pins(spi_t bus);
206 
226 int spi_init_cs(spi_t bus, spi_cs_t cs);
227 
249 int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk);
250 
259 void spi_release(spi_t bus);
260 
272 uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out);
273 
285 void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
286  const void *out, void *in, size_t len);
287 
302 uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out);
303 
318 void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg,
319  const void *out, void *in, size_t len);
320 
321 #ifdef __cplusplus
322 }
323 #endif
324 
325 #endif /* PERIPH_SPI_H */
326 
CPOL=0, CPHA=1.
Definition: spi.h:146
selected clock value is not supported
Definition: spi.h:124
Low-level GPIO peripheral driver interface definitions.
CPOL=0, CPHA=0.
Definition: spi.h:145
drive the SPI bus with 100KHz
Definition: spi.h:161
drive the SPI bus with 400KHz
Definition: spi.h:162
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:113
spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: spi.h:144
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:160
CPOL=1, CPHA=1.
Definition: spi.h:148
everything went as planned
Definition: spi.h:120
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:164
drive the SPI bus with 10MHz
Definition: spi.h:165
drive the SPI bus with 1MHz
Definition: spi.h:163
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.
int spi_init_cs(spi_t bus, spi_cs_t cs)
Initialize the given chip select pin.
void spi_init_pins(spi_t bus)
Initialize the used SPI bus pins, i.e.
CPOL=1, CPHA=0.
Definition: spi.h:147
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.
invalid SPI bus specified
Definition: spi.h:121
selected mode is not supported
Definition: spi.h:123
invalid chip select line specified
Definition: spi.h:122
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:105