MySensors Library & Examples  2.3.2
SPIDEV.h
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2019 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  * Based on TMRh20 RF24 library, Copyright (c) 2015 Charles-Henri Hallard <[email protected]>
20  */
21 
22 #ifndef SPIDEV_h
23 #define SPIDEV_h
24 
25 #include <stdint.h>
26 #include <string>
27 #include <linux/spi/spidev.h>
28 
29 #define SPI_HAS_TRANSACTION
30 
31 #define MSBFIRST 0
32 #define LSBFIRST SPI_LSB_FIRST
33 
34 #define SPI_CLOCK_BASE 16000000 // 16Mhz
35 
36 #define SPI_CLOCK_DIV1 1
37 #define SPI_CLOCK_DIV2 2
38 #define SPI_CLOCK_DIV4 4
39 #define SPI_CLOCK_DIV8 8
40 #define SPI_CLOCK_DIV16 16
41 #define SPI_CLOCK_DIV32 32
42 #define SPI_CLOCK_DIV64 64
43 #define SPI_CLOCK_DIV128 128
44 #define SPI_CLOCK_DIV256 256
45 
46 // SPI Data mode
47 #define SPI_MODE0 SPI_MODE_0
48 #define SPI_MODE1 SPI_MODE_1
49 #define SPI_MODE2 SPI_MODE_2
50 #define SPI_MODE3 SPI_MODE_3
51 
52 #ifndef SPI_SPIDEV_DEVICE
53 #define SPI_SPIDEV_DEVICE "/dev/spidev0.0"
54 #endif
55 
56 // Default to Raspberry Pi
57 const uint8_t SS = 24;
58 const uint8_t MOSI = 19;
59 const uint8_t MISO = 21;
60 const uint8_t SCK = 23;
61 
65 class SPISettings
66 {
67 
68 public:
73  {
74  init(SPI_CLOCK_BASE, MSBFIRST, SPI_MODE0);
75  }
83  SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
84  {
85  init(clock, bitOrder, dataMode);
86  }
87 
88  uint32_t clock;
89  uint8_t border;
90  uint8_t dmode;
91 
92 private:
100  void init(uint32_t clk, uint8_t bitOrder, uint8_t dataMode)
101  {
102  clock = clk;
103  border = bitOrder;
104  dmode = dataMode;
105  }
106 
107  friend class SPIDEVClass;
108 };
109 
114 {
115 
116 public:
120  SPIDEVClass();
124  static void begin(int busNo=0);
128  static void end();
134  static void setBitOrder(uint8_t bit_order);
140  static void setDataMode(uint8_t data_mode);
146  static void setClockDivider(uint16_t divider);
152  static void chipSelect(int csn_chip);
159  static uint8_t transfer(uint8_t data);
167  static void transfernb(char* tbuf, char* rbuf, uint32_t len);
174  static void transfern(char* buf, uint32_t len);
180  static void beginTransaction(SPISettings settings);
184  static void endTransaction();
190  static void usingInterrupt(uint8_t interruptNumber);
196  static void notUsingInterrupt(uint8_t interruptNumber);
197 
198 private:
199  static uint8_t initialized;
200  static int fd;
201  static std::string device;
202  static uint8_t mode;
203  static uint32_t speed;
204  static uint8_t bit_order;
205  static struct spi_ioc_transfer tr;
206 
207  static void init();
208 };
209 
210 extern SPIDEVClass SPIDEV;
211 
212 #endif
SPIDEVClass::setBitOrder
static void setBitOrder(uint8_t bit_order)
Sets the SPI bit order.
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:653
SPIDEVClass::end
static void end()
End SPI operations.
SPIDEVClass::endTransaction
static void endTransaction()
End SPI transaction.
SPISettings::clock
uint32_t clock
SPI clock.
Definition: SPIDEV.h:88
SPIDEVClass::transfern
static void transfern(char *buf, uint32_t len)
Transfer a buffer of data without an rx buffer.
SPIDEVClass::transfernb
static void transfernb(char *tbuf, char *rbuf, uint32_t len)
Transfer a buffer of data.
SPIDEVClass::SPIDEVClass
SPIDEVClass()
SPIDEVClass constructor.
SPISettings::SPISettings
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
SPISettings constructor.
Definition: SPIDEV.h:83
SPIDEVClass::usingInterrupt
static void usingInterrupt(uint8_t interruptNumber)
Not implemented.
SPISettings
Definition: SPIBCM.h:69
SPISettings::border
uint8_t border
SPI bit order.
Definition: SPIBCM.h:136
SPIDEVClass
Definition: SPIDEV.h:113
SPISettings::SPISettings
SPISettings()
SPISettings constructor.
Definition: SPIDEV.h:72
SPIDEVClass::setDataMode
static void setDataMode(uint8_t data_mode)
Sets the SPI data mode.
SPIDEVClass::beginTransaction
static void beginTransaction(SPISettings settings)
Start SPI transaction.
SPISettings::dmode
uint8_t dmode
SPI data mode.
Definition: SPIBCM.h:137
SPIDEVClass::transfer
static uint8_t transfer(uint8_t data)
Transfer a single byte.
SPIDEVClass::chipSelect
static void chipSelect(int csn_chip)
Sets the chip select pin.
SPIDEVClass::begin
static void begin(int busNo=0)
Start SPI operations.
SPIDEVClass::setClockDivider
static void setClockDivider(uint16_t divider)
Sets the SPI clock divider and therefore the SPI clock speed.
SPIDEVClass::notUsingInterrupt
static void notUsingInterrupt(uint8_t interruptNumber)
Not implemented.