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 
9 #pragma once
10 
30 #include "periph/gpio.h"
31 #include "periph/spi.h"
32 #include "mutex.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
46 #ifndef SOFT_SPI_DEV
47 #define SOFT_SPI_DEV(x) (x)
48 #endif
49 
53 #ifndef SOFT_SPI_UNDEF
54 #define SOFT_SPI_UNDEF (UINT_MAX)
55 #endif
56 
60 #ifndef SOFT_SPI_CS_UNDEF
61 #define SOFT_SPI_CS_UNDEF (GPIO_UNDEF)
62 #endif
68 typedef unsigned int soft_spi_t;
69 
74 typedef gpio_t soft_spi_cs_t;
75 
79 enum {
84  SOFT_SPI_NOCLK = -4
85 };
86 
103 typedef enum {
109 
117 typedef enum {
122 
126 typedef struct {
127  gpio_t miso_pin;
128  gpio_t mosi_pin;
129  gpio_t clk_pin;
133 
148 
160 
175 
193 
203 
216 uint8_t soft_spi_transfer_byte(soft_spi_t bus, soft_spi_cs_t cs, bool cont, uint8_t out);
217 
229  const void *out, void *in, size_t len);
230 
244 uint8_t soft_spi_transfer_reg(soft_spi_t bus, soft_spi_cs_t cs, uint8_t reg, uint8_t out);
245 
260  const void *out, void *in, size_t len);
261 
262 #ifdef __cplusplus
263 }
264 #endif
265 
Low-level GPIO peripheral driver interface definitions.
soft_spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: soft_spi.h:103
int soft_spi_init_cs(soft_spi_t bus, soft_spi_cs_t cs)
Initialize the given chip select pin.
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.
unsigned int soft_spi_t
Default type for SPI devices.
Definition: soft_spi.h:68
void soft_spi_init(soft_spi_t bus)
Basic initialization of the given SPI bus.
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:74
void soft_spi_init_pins(soft_spi_t bus)
Initialize the used SPI bus pins, i.e.
void soft_spi_release(soft_spi_t bus)
Finish an ongoing SPI transaction by releasing the given SPI bus.
void 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.
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.
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.
soft_spi_clk_t
Available SPI clock speeds.
Definition: soft_spi.h:117
@ SOFT_SPI_MODE_2
CPOL=1, CPHA=0.
Definition: soft_spi.h:106
@ SOFT_SPI_MODE_3
CPOL=1, CPHA=1.
Definition: soft_spi.h:107
@ SOFT_SPI_MODE_1
CPOL=0, CPHA=1.
Definition: soft_spi.h:105
@ SOFT_SPI_MODE_0
CPOL=0, CPHA=0.
Definition: soft_spi.h:104
@ SOFT_SPI_OK
everything went as planned
Definition: soft_spi.h:80
@ SOFT_SPI_NOCLK
selected clock value is not supported
Definition: soft_spi.h:84
@ SOFT_SPI_NOMODE
selected mode is not supported
Definition: soft_spi.h:83
@ SOFT_SPI_NOCS
invalid chip select line specified
Definition: soft_spi.h:82
@ SOFT_SPI_NODEV
invalid SPI bus specified
Definition: soft_spi.h:81
@ SOFT_SPI_CLK_1MHZ
drive the SPI bus with less than 1MHz
Definition: soft_spi.h:119
@ SOFT_SPI_CLK_100KHZ
drive the SPI bus with less than 100kHz
Definition: soft_spi.h:118
@ SOFT_SPI_CLK_DEFAULT
drive the SPI bus with maximum speed possible
Definition: soft_spi.h:120
Mutex for thread synchronization.
Low-level SPI peripheral driver interface definition.
Software SPI port descriptor.
Definition: soft_spi.h:126
soft_spi_mode_t soft_spi_mode
data and clock polarity
Definition: soft_spi.h:130
gpio_t clk_pin
CLK pin.
Definition: soft_spi.h:129
gpio_t miso_pin
MOSI pin.
Definition: soft_spi.h:127
soft_spi_clk_t soft_spi_clk
clock speed
Definition: soft_spi.h:131
gpio_t mosi_pin
MOSI pin.
Definition: soft_spi.h:128