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)
64 class SPISettings {
65 private:
66  spi_mode_t mode;
67  spi_clk_t clock;
68 
69 public:
82  SPISettings(uint32_t clock_hz, uint8_t bitOrder, uint8_t dataMode);
83 
88 
89  friend class SPIClass;
90 };
91 
103 class SPIClass {
104 private:
105  spi_t spi_dev;
106  SPISettings settings;
107  bool is_transaction;
108  rmutex_t mut;
109 
110 public:
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 
189  void beginTransaction(SPISettings settings);
190 
194  void endTransaction();
195 
207  void setBitOrder(uint8_t order);
208 
217  void setDataMode(uint8_t mode);
218 
227  void setClockDivider(uint8_t divider);
228 };
229 
233 extern SPIClass SPI;
234 
235 #endif /* SPIPORT_H */
236 
SPI
SPIClass SPI
: Instance of the SPI interface as required by the Arduino API
SPIClass
Arduino SPI interface.
Definition: spiport.hpp:103
byteorder.h
Functions to work with different byte orders.
SPIClass::setClockDivider
void setClockDivider(uint8_t divider)
Sets the SPI clock in an archaic manner.
spi.h
Low-level SPI peripheral driver interface definition.
MSBFIRST
#define MSBFIRST
most significat bit first
Definition: spiport.hpp:58
SPIClass::setDataMode
void setDataMode(uint8_t mode)
Sets the SPI mode (clock phase and polarity)
ntohs
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:488
SPIClass::transfer
uint8_t transfer(uint8_t data)
Transfer a single byte of data.
Definition: spiport.hpp:140
SPI_DEV
#define SPI_DEV(x)
Default SPI device access macro.
Definition: spi.h:86
SPIClass::endTransaction
void endTransaction()
Releases the SPI interface.
SPISettings
Arduino SPI configuration interface.
Definition: spiport.hpp:64
SPIClass::SPIClass
SPIClass(spi_t spi_dev)
Create a new SPI interface instance.
spi_mode_t
spi_mode_t
Available SPI modes, defining the configuration of clock polarity and clock phase.
Definition: spi.h:157
spi_clk_t
spi_clk_t
Available SPI clock speeds.
Definition: spi.h:173
SPIClass::setBitOrder
void setBitOrder(uint8_t order)
Sets the bit order to the given value.
rmutex_t
Mutex structure.
Definition: rmutex.h:43
SPISettings::SPISettings
SPISettings()
Create a new SPI settings instance with default settings.
Definition: spiport.hpp:87
rmutex.h
Recursive Mutex for thread synchronization.
htons
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:473
SPIClass::begin
void begin()
Doesn't do anything, for compatibility only.
Definition: spiport.hpp:178
SPIClass::beginTransaction
void beginTransaction(SPISettings settings)
Acquires the SPI interface and applies the given settings.
SPI_MODE0
#define SPI_MODE0
CPOL=0, CPHA=0.
Definition: spiport.hpp:31
SPIClass::end
void end()
Doesn't do anything, for compatibility only.
Definition: spiport.hpp:183
SPIClass::transfer16
uint16_t transfer16(uint16_t data)
Transfer two bytes of data.
Definition: spiport.hpp:160
spi_t
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:118
SPIClass::SPIClass
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