Loading...
Searching...
No Matches
spiport.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Otto-von-Guericke-Universität Magdeburg
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
19#ifndef SPIPORT_H
20#define SPIPORT_H
21
22#include "arduino_board.h"
23#include "byteorder.h"
24#include "periph/spi.h"
25#include "rmutex.h"
26
31#define SPI_MODE0 (0)
32#define SPI_MODE1 (1)
33#define SPI_MODE2 (2)
34#define SPI_MODE3 (3)
45#define SPI_CLOCK_DIV2 (0)
46#define SPI_CLOCK_DIV4 (1)
47#define SPI_CLOCK_DIV8 (1)
48#define SPI_CLOCK_DIV16 (1)
49#define SPI_CLOCK_DIV32 (2)
50#define SPI_CLOCK_DIV64 (3)
51#define SPI_CLOCK_DIV128 (3)
58#define MSBFIRST (1)
65private:
66 spi_mode_t mode;
67 spi_clk_t clock;
68
69public:
82 SPISettings(uint32_t clock_hz, uint8_t bitOrder, uint8_t dataMode);
83
88
89 friend class SPIClass;
90};
91
103class SPIClass {
104private:
105 spi_t spi_dev;
106 SPISettings settings;
107 bool is_transaction;
108 rmutex_t mut;
109
110public:
115 explicit SPIClass(spi_t spi_dev);
116
125 SPIClass(uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI,
126 uint8_t uc_pinSS, uint8_t uc_mux) : SPIClass(SPI_DEV(0))
127 {
128 (void)uc_pinMISO;
129 (void)uc_pinSCK;
130 (void)uc_pinMOSI;
131 (void)uc_pinSS;
132 (void)uc_mux;
133 }
134
140 uint8_t transfer(uint8_t data)
141 {
142 transfer(&data, sizeof(data));
143 return data;
144 }
145
160 uint16_t transfer16(uint16_t data)
161 {
162 data = htons(data);
163 transfer(&data, sizeof(data));
164 return ntohs(data);
165 }
166
173 void transfer(void *buf, size_t count);
174
178 void begin() { }
179
183 void end() { }
184
190
195
207 void setBitOrder(uint8_t order);
208
217 void setDataMode(uint8_t mode);
218
227 void setClockDivider(uint8_t divider);
228};
229
233extern SPIClass SPI;
234
235#endif /* SPIPORT_H */
Configuration of the Arduino API for Arduino Atmega boards.
spi_clk_t
Definition periph_cpu.h:352
Functions to work with different byte orders.
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:536
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:521
Arduino SPI interface.
Definition spiport.hpp:103
void setDataMode(uint8_t mode)
Sets the SPI mode (clock phase and polarity)
void begin()
Doesn't do anything, for compatibility only.
Definition spiport.hpp:178
void setClockDivider(uint8_t divider)
Sets the SPI clock in an archaic manner.
uint16_t transfer16(uint16_t data)
Transfer two bytes of data.
Definition spiport.hpp:160
void end()
Doesn't do anything, for compatibility only.
Definition spiport.hpp:183
SPIClass(spi_t spi_dev)
Create a new SPI interface instance.
SPIClass(uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI, uint8_t uc_pinSS, uint8_t uc_mux)
Create a new SPI interface instance for SPI device 0.
Definition spiport.hpp:125
void setBitOrder(uint8_t order)
Sets the bit order to the given value.
void endTransaction()
Releases the SPI interface.
void beginTransaction(SPISettings settings)
Acquires the SPI interface and applies the given settings.
void transfer(void *buf, size_t count)
Transfer data.
uint8_t transfer(uint8_t data)
Transfer a single byte of data.
Definition spiport.hpp:140
Arduino SPI configuration interface.
Definition spiport.hpp:64
SPISettings()
Create a new SPI settings instance with default settings.
Definition spiport.hpp:87
SPISettings(uint32_t clock_hz, uint8_t bitOrder, uint8_t dataMode)
Create a new SPI settings instance.
#define SPI_DEV(x)
Default SPI device access macro.
Definition spi.h:95
spi_mode_t
Support SPI modes.
Definition periph_cpu.h:43
Recursive Mutex for thread synchronization.
Low-level SPI peripheral driver interface definition.
#define MSBFIRST
most significant bit first
Definition spiport.hpp:58
SPIClass SPI
: Instance of the SPI interface as required by the Arduino API
#define SPI_MODE0
CPOL=0, CPHA=0.
Definition spiport.hpp:31
Mutex structure.
Definition rmutex.h:38