soft_spi.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Hamburg University of Applied Sciences
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 
28 #ifndef SOFT_SPI_H
29 #define SOFT_SPI_H
30 
31 #include "periph/gpio.h"
32 #include "periph/spi.h"
33 #include "mutex.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
42 #ifndef SOFT_SPI_DEV
43 #define SOFT_SPI_DEV(x) (x)
44 #endif
45 
49 #ifndef SOFT_SPI_UNDEF
50 #define SOFT_SPI_UNDEF (UINT_MAX)
51 #endif
52 
56 #ifndef SOFT_SPI_CS_UNDEF
57 #define SOFT_SPI_CS_UNDEF (GPIO_UNDEF)
58 #endif
59 
63 typedef unsigned int soft_spi_t;
64 
69 typedef gpio_t soft_spi_cs_t;
70 
74 enum {
80 };
81 
98 typedef enum {
104 
112 typedef enum {
117 
121 typedef struct {
122  gpio_t miso_pin;
123  gpio_t mosi_pin;
124  gpio_t clk_pin;
125  soft_spi_mode_t soft_spi_mode;
128 
142 void soft_spi_init(soft_spi_t bus);
143 
154 void soft_spi_init_pins(soft_spi_t bus);
155 
169 int soft_spi_init_cs(soft_spi_t bus, soft_spi_cs_t cs);
170 
191 int soft_spi_acquire(soft_spi_t bus, soft_spi_cs_t cs, soft_spi_mode_t mode, soft_spi_clk_t clk);
192 
201 void soft_spi_release(soft_spi_t bus);
202 
215 uint8_t soft_spi_transfer_byte(soft_spi_t bus, soft_spi_cs_t cs, bool cont, uint8_t out);
216 
227 void soft_spi_transfer_bytes(soft_spi_t bus, soft_spi_cs_t cs, bool cont,
228  const void *out, void *in, size_t len);
229 
243 uint8_t soft_spi_transfer_reg(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg, uint8_t out);
244 
258 void soft_spi_transfer_regs(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg,
259  const void *out, void *in, size_t len);
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 #endif /* SOFT_SPI_H */
266 
invalid chip select line specified
Definition: soft_spi.h:77
soft_spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: soft_spi.h:98
Software SPI port descriptor.
Definition: soft_spi.h:121
invalid SPI bus specified
Definition: soft_spi.h:76
selected clock value is not supported
Definition: soft_spi.h:79
gpio_t mosi_pin
MOSI pin.
Definition: soft_spi.h:123
gpio_t soft_spi_cs_t
Chip select pin type overlaps with gpio_t so it can be casted to this.
Definition: soft_spi.h:69
Low-level GPIO peripheral driver interface definitions.
uint8_t soft_spi_transfer_reg(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg, uint8_t out)
Transfer one byte to/from a given register address.
gpio_t clk_pin
CLK pin.
Definition: soft_spi.h:124
int soft_spi_init_cs(soft_spi_t bus, soft_spi_cs_t cs)
Initialize the given chip select pin.
uint8_t soft_spi_transfer_byte(soft_spi_t bus, soft_spi_cs_t cs, bool cont, uint8_t out)
Transfer one byte on the given SPI bus Currently only the use of MOSI in master mode is implemented...
void soft_spi_transfer_bytes(soft_spi_t bus, soft_spi_cs_t cs, bool cont, const void *out, void *in, size_t len)
Transfer a number bytes using the given SPI bus.
Low-level SPI peripheral driver interface definition.
everything went as planned
Definition: soft_spi.h:75
drive the SPI bus with less than 100kHz
Definition: soft_spi.h:113
unsigned int soft_spi_t
Default type for SPI devices.
Definition: soft_spi.h:63
soft_spi_mode_t soft_spi_mode
data and clock polarity
Definition: soft_spi.h:125
void soft_spi_transfer_regs(soft_spi_t bus, soft_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.
CPOL=0, CPHA=0.
Definition: soft_spi.h:99
soft_spi_clk_t
Available SPI clock speeds.
Definition: soft_spi.h:112
void soft_spi_init(soft_spi_t bus)
Basic initialization of the given SPI bus.
CPOL=1, CPHA=0.
Definition: soft_spi.h:101
void soft_spi_release(soft_spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
gpio_t miso_pin
MOSI pin.
Definition: soft_spi.h:122
RIOT synchronization API.
drive the SPI bus with less than 400kHz
Definition: soft_spi.h:114
drive the SPI bus with maximum speed possible
Definition: soft_spi.h:115
selected mode is not supported
Definition: soft_spi.h:78
soft_spi_clk_t soft_spi_clk
clock speed
Definition: soft_spi.h:126
CPOL=0, CPHA=1.
Definition: soft_spi.h:100
void soft_spi_init_pins(soft_spi_t bus)
Initialize the used SPI bus pins, i.e.
int soft_spi_acquire(soft_spi_t bus, soft_spi_cs_t cs, soft_spi_mode_t mode, soft_spi_clk_t clk)
Start a new SPI transaction.
CPOL=1, CPHA=1.
Definition: soft_spi.h:102