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 
9 #pragma once
10 
69 #include <endian.h>
70 #include <errno.h>
71 #include <stdbool.h>
72 #include <stddef.h>
73 #include <stdint.h>
74 
75 #include "periph_cpu.h"
76 #include "periph_conf.h"
77 #include "periph/gpio.h"
78 
79 #ifdef __cplusplus
80 extern "C" {
81 #endif
82 
87 #ifndef CONFIG_SPI_DMA_THRESHOLD_BYTES
88 #define CONFIG_SPI_DMA_THRESHOLD_BYTES 16
89 #endif
90 
94 #ifndef SPI_DEV
95 #define SPI_DEV(x) (x)
96 #endif
97 
101 #ifndef SPI_UNDEF
102 #define SPI_UNDEF (UINT_FAST8_MAX)
103 #endif
104 
108 #ifndef SPI_CS_UNDEF
109 #define SPI_CS_UNDEF (GPIO_UNDEF)
110 #endif
111 
119 #ifndef SPI_HWCS
120 #define SPI_HWCS(x) (SPI_CS_UNDEF)
121 #endif
122 
126 #ifndef HAVE_SPI_T
127 typedef uint_fast8_t spi_t;
128 #endif
129 
134 #ifndef HAVE_SPI_CS_T
135 typedef gpio_t spi_cs_t;
136 #endif
137 
144 enum {
145  SPI_OK = 0,
149  SPI_NOCLK = -EINVAL
150 };
151 
168 #ifndef HAVE_SPI_MODE_T
169 typedef enum {
173  SPI_MODE_3
175 #endif
176 
184 #ifndef HAVE_SPI_CLK_T
185 typedef enum {
192 #endif
193 
216 void spi_init(spi_t bus);
217 
235 void spi_init_pins(spi_t bus);
236 
256 int spi_init_cs(spi_t bus, spi_cs_t cs);
257 
258 #if defined(MODULE_PERIPH_SPI_RECONFIGURE) || DOXYGEN
259 
280 void spi_deinit_pins(spi_t dev);
281 
282 #if DOXYGEN
283 
294 gpio_t spi_pin_miso(spi_t dev);
295 
306 gpio_t spi_pin_mosi(spi_t dev);
307 
318 gpio_t spi_pin_clk(spi_t dev);
319 
320 #endif /* DOXYGEN */
321 #endif /* MODULE_PERIPH_SPI_RECONFIGURE */
322 
323 #if defined(MODULE_PERIPH_SPI_GPIO_MODE) || DOXYGEN
324 
328 typedef struct {
333 
344 int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t* mode);
345 #endif
346 
364 void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk);
365 
374 void spi_release(spi_t bus);
375 
387 uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out);
388 
400 void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
401  const void *out, void *in, size_t len);
402 
417 uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out);
418 
433 void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg,
434  const void *out, void *in, size_t len);
435 
447 static inline uint16_t spi_transfer_u16_be(spi_t bus, spi_cs_t cs, bool cont, uint16_t host_number)
448 {
449  const uint16_t send = htobe16(host_number);
450  uint16_t receive;
451  spi_transfer_bytes(bus, cs, cont, &send, &receive, sizeof(receive));
452  return be16toh(receive);
453 }
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
spi_clk_t
Definition: periph_cpu.h:351
libc header for endian conversion
Low-level GPIO peripheral driver interface definitions.
#define ENXIO
No such device or address.
Definition: errno.h:131
#define EINVAL
Invalid argument.
Definition: errno.h:96
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
uint_fast8_t spi_t
Default type for SPI devices.
Definition: spi.h:127
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:447
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:169
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:185
gpio_t spi_cs_t
Chip select pin type overlaps with gpio_t so it can be casted to this.
Definition: spi.h:135
@ SPI_OK
everything went as planned
Definition: spi.h:145
@ SPI_NOCS
invalid chip select line specified
Definition: spi.h:147
@ SPI_NOCLK
selected clock value is not supported
Definition: spi.h:149
@ SPI_NODEV
invalid SPI bus specified
Definition: spi.h:146
@ SPI_NOMODE
selected mode is not supported
Definition: spi.h:148
@ SPI_MODE_0
CPOL=0, CPHA=0.
Definition: spi.h:170
@ SPI_MODE_2
CPOL=1, CPHA=0.
Definition: spi.h:172
@ SPI_MODE_1
CPOL=0, CPHA=1.
Definition: spi.h:171
@ SPI_MODE_3
CPOL=1, CPHA=1.
Definition: spi.h:173
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: spi.h:190
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: spi.h:189
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: spi.h:187
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: spi.h:188
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: spi.h:186
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:42
SPI gpio mode.
Definition: spi.h:328
gpio_mode_t mosi
GPIO mode used for MOSI pin.
Definition: spi.h:329
gpio_mode_t miso
GPIO mode used for MISO pin.
Definition: spi.h:330
gpio_mode_t sclk
GPIO mode used for SCLK pin.
Definition: spi.h:331