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 
47 #ifndef SOFT_SPI_DEV
48 #define SOFT_SPI_DEV(x) (x)
49 #endif
50 
54 #ifndef SOFT_SPI_UNDEF
55 #define SOFT_SPI_UNDEF (UINT_MAX)
56 #endif
57 
61 #ifndef SOFT_SPI_CS_UNDEF
62 #define SOFT_SPI_CS_UNDEF (GPIO_UNDEF)
63 #endif
64 
69 typedef unsigned int soft_spi_t;
70 
75 typedef gpio_t soft_spi_cs_t;
76 
80 enum {
86 };
87 
104 typedef enum {
110 
118 typedef enum {
123 
127 typedef struct {
128  gpio_t miso_pin;
129  gpio_t mosi_pin;
130  gpio_t clk_pin;
134 
148 void soft_spi_init(soft_spi_t bus);
149 
161 
176 
198 
207 void soft_spi_release(soft_spi_t bus);
208 
221 uint8_t soft_spi_transfer_byte(soft_spi_t bus, soft_spi_cs_t cs, bool cont, uint8_t out);
222 
233 void soft_spi_transfer_bytes(soft_spi_t bus, soft_spi_cs_t cs, bool cont,
234  const void *out, void *in, size_t len);
235 
249 uint8_t soft_spi_transfer_reg(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg, uint8_t out);
250 
264 void soft_spi_transfer_regs(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg,
265  const void *out, void *in, size_t len);
266 
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif /* SOFT_SPI_H */
272 
soft_spi_conf_t::clk_pin
gpio_t clk_pin
CLK pin.
Definition: soft_spi.h:130
soft_spi_conf_t::soft_spi_mode
soft_spi_mode_t soft_spi_mode
data and clock polarity
Definition: soft_spi.h:131
spi.h
Low-level SPI peripheral driver interface definition.
soft_spi_transfer_bytes
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.
SOFT_SPI_MODE_2
@ SOFT_SPI_MODE_2
CPOL=1, CPHA=0.
Definition: soft_spi.h:107
SOFT_SPI_OK
@ SOFT_SPI_OK
everything went as planned
Definition: soft_spi.h:81
soft_spi_transfer_byte
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.
soft_spi_init
void soft_spi_init(soft_spi_t bus)
Basic initialization of the given SPI bus.
SOFT_SPI_NOMODE
@ SOFT_SPI_NOMODE
selected mode is not supported
Definition: soft_spi.h:84
SOFT_SPI_CLK_400KHZ
@ SOFT_SPI_CLK_400KHZ
drive the SPI bus with less than 400kHz
Definition: soft_spi.h:120
SOFT_SPI_CLK_100KHZ
@ SOFT_SPI_CLK_100KHZ
drive the SPI bus with less than 100kHz
Definition: soft_spi.h:119
soft_spi_conf_t::mosi_pin
gpio_t mosi_pin
MOSI pin.
Definition: soft_spi.h:129
soft_spi_conf_t::miso_pin
gpio_t miso_pin
MOSI pin.
Definition: soft_spi.h:128
soft_spi_clk_t
soft_spi_clk_t
Available SPI clock speeds.
Definition: soft_spi.h:118
soft_spi_init_pins
void soft_spi_init_pins(soft_spi_t bus)
Initialize the used SPI bus pins, i.e.
soft_spi_mode_t
soft_spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: soft_spi.h:104
soft_spi_cs_t
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:75
SOFT_SPI_MODE_1
@ SOFT_SPI_MODE_1
CPOL=0, CPHA=1.
Definition: soft_spi.h:106
soft_spi_transfer_reg
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.
mutex.h
Mutex for thread synchronization.
soft_spi_transfer_regs
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.
soft_spi_release
void soft_spi_release(soft_spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
gpio.h
Low-level GPIO peripheral driver interface definitions.
soft_spi_acquire
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.
soft_spi_init_cs
int soft_spi_init_cs(soft_spi_t bus, soft_spi_cs_t cs)
Initialize the given chip select pin.
SOFT_SPI_MODE_0
@ SOFT_SPI_MODE_0
CPOL=0, CPHA=0.
Definition: soft_spi.h:105
SOFT_SPI_MODE_3
@ SOFT_SPI_MODE_3
CPOL=1, CPHA=1.
Definition: soft_spi.h:108
SOFT_SPI_CLK_DEFAULT
@ SOFT_SPI_CLK_DEFAULT
drive the SPI bus with maximum speed possible
Definition: soft_spi.h:121
soft_spi_conf_t
Software SPI port descriptor.
Definition: soft_spi.h:127
SOFT_SPI_NODEV
@ SOFT_SPI_NODEV
invalid SPI bus specified
Definition: soft_spi.h:82
soft_spi_conf_t::soft_spi_clk
soft_spi_clk_t soft_spi_clk
clock speed
Definition: soft_spi.h:132
soft_spi_t
unsigned int soft_spi_t
Default type for SPI devices.
Definition: soft_spi.h:69
SOFT_SPI_NOCS
@ SOFT_SPI_NOCS
invalid chip select line specified
Definition: soft_spi.h:83
SOFT_SPI_NOCLK
@ SOFT_SPI_NOCLK
selected clock value is not supported
Definition: soft_spi.h:85