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 
27 #ifndef SOFT_SPI_H
28 #define SOFT_SPI_H
29 
30 #include "periph/gpio.h"
31 #include "periph/spi.h"
32 #include "mutex.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
41 #ifndef SOFT_SPI_DEV
42 #define SOFT_SPI_DEV(x) (x)
43 #endif
44 
48 #ifndef SOFT_SPI_UNDEF
49 #define SOFT_SPI_UNDEF (UINT_MAX)
50 #endif
51 
55 #ifndef SOFT_SPI_CS_UNDEF
56 #define SOFT_SPI_CS_UNDEF (GPIO_UNDEF)
57 #endif
58 
62 typedef unsigned int soft_spi_t;
63 
68 typedef gpio_t soft_spi_cs_t;
69 
73 enum {
79 };
80 
97 typedef enum {
103 
111 typedef enum {
116 
120 typedef struct {
121  gpio_t miso_pin;
122  gpio_t mosi_pin;
123  gpio_t clk_pin;
124  soft_spi_mode_t soft_spi_mode;
127 
141 void soft_spi_init(soft_spi_t bus);
142 
153 void soft_spi_init_pins(soft_spi_t bus);
154 
168 int soft_spi_init_cs(soft_spi_t bus, soft_spi_cs_t cs);
169 
190 int soft_spi_acquire(soft_spi_t bus, soft_spi_cs_t cs, soft_spi_mode_t mode, soft_spi_clk_t clk);
191 
200 void soft_spi_release(soft_spi_t bus);
201 
214 uint8_t soft_spi_transfer_byte(soft_spi_t bus, soft_spi_cs_t cs, bool cont, uint8_t out);
215 
226 void soft_spi_transfer_bytes(soft_spi_t bus, soft_spi_cs_t cs, bool cont,
227  const void *out, void *in, size_t len);
228 
242 uint8_t soft_spi_transfer_reg(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg, uint8_t out);
243 
257 void soft_spi_transfer_regs(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg,
258  const void *out, void *in, size_t len);
259 
260 #ifdef __cplusplus
261 }
262 #endif
263 
264 #endif /* SOFT_SPI_H */
265 
Software SPI port descriptor.
Definition: soft_spi.h:120
drive the SPI bus with less than 100kHz
Definition: soft_spi.h:112
void soft_spi_init_pins(soft_spi_t bus)
Initialize the used SPI bus pins, i.e.
gpio_t mosi_pin
MOSI pin.
Definition: soft_spi.h:122
Low-level GPIO peripheral driver interface definitions.
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_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:123
drive the SPI bus with maximum speed possible
Definition: soft_spi.h:114
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.
selected clock value is not supported
Definition: soft_spi.h:78
Low-level SPI peripheral driver interface definition.
drive the SPI bus with less than 400kHz
Definition: soft_spi.h:113
soft_spi_mode_t soft_spi_mode
data and clock polarity
Definition: soft_spi.h:124
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:68
CPOL=0, CPHA=0.
Definition: soft_spi.h:98
everything went as planned
Definition: soft_spi.h:74
CPOL=0, CPHA=1.
Definition: soft_spi.h:99
soft_spi_clk_t
Available SPI clock speeds.
Definition: soft_spi.h:111
gpio_t miso_pin
MOSI pin.
Definition: soft_spi.h:121
soft_spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: soft_spi.h:97
RIOT synchronization API.
selected mode is not supported
Definition: soft_spi.h:77
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_init(soft_spi_t bus)
Basic initialization of the given SPI bus.
void soft_spi_release(soft_spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
soft_spi_clk_t soft_spi_clk
clock speed
Definition: soft_spi.h:125
invalid SPI bus specified
Definition: soft_spi.h:75
CPOL=1, CPHA=1.
Definition: soft_spi.h:101
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=0.
Definition: soft_spi.h:100
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.
invalid chip select line specified
Definition: soft_spi.h:76
unsigned int soft_spi_t
Default type for SPI devices.
Definition: soft_spi.h:62