Loading...
Searching...
No Matches
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
66#ifndef PERIPH_SPI_H
67#define PERIPH_SPI_H
68
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
80extern "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
127typedef uint_fast8_t spi_t;
128#endif
129
134#ifndef HAVE_SPI_CS_T
135typedef gpio_t spi_cs_t;
136#endif
137
144enum {
145 SPI_OK = 0,
149 SPI_NOCLK = -EINVAL
151
168#ifndef HAVE_SPI_MODE_T
175#endif
176
184#ifndef HAVE_SPI_CLK_T
192#endif
193
213void spi_init(spi_t bus);
214
232void spi_init_pins(spi_t bus);
233
253int spi_init_cs(spi_t bus, spi_cs_t cs);
254
255#if defined(MODULE_PERIPH_SPI_RECONFIGURE) || DOXYGEN
256
277void spi_deinit_pins(spi_t dev);
278
279#if DOXYGEN
280
291gpio_t spi_pin_miso(spi_t dev);
292
303gpio_t spi_pin_mosi(spi_t dev);
304
315gpio_t spi_pin_clk(spi_t dev);
316
317#endif /* DOXYGEN */
318#endif /* MODULE_PERIPH_SPI_RECONFIGURE */
319
320#if defined(MODULE_PERIPH_SPI_GPIO_MODE) || DOXYGEN
321
330
341int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t* mode);
342#endif
343
361void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk);
362
371void spi_release(spi_t bus);
372
384uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out);
385
397void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
398 const void *out, void *in, size_t len);
399
414uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out);
415
430void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg,
431 const void *out, void *in, size_t len);
432
444static inline uint16_t spi_transfer_u16_be(spi_t bus, spi_cs_t cs, bool cont, uint16_t host_number)
445{
446 const uint16_t send = htobe16(host_number);
447 uint16_t receive;
448 spi_transfer_bytes(bus, cs, cont, &send, &receive, sizeof(receive));
449 return be16toh(receive);
450}
451
452#ifdef __cplusplus
453}
454#endif
455
456#endif /* PERIPH_SPI_H */
spi_clk_t
Definition periph_cpu.h:352
Low-level GPIO peripheral driver interface definitions.
libc header for endian conversion
#define ENXIO
No such device or address.
Definition errno.h:132
#define EINVAL
Invalid argument.
Definition errno.h:97
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:444
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:43
gpio_mode_t
Available pin modes.
Definition periph_cpu.h:82
SPI gpio mode.
Definition spi.h:325
gpio_mode_t mosi
GPIO mode used for MOSI pin.
Definition spi.h:326
gpio_mode_t miso
GPIO mode used for MISO pin.
Definition spi.h:327
gpio_mode_t sclk
GPIO mode used for SCLK pin.
Definition spi.h:328