SPIBase.h
Go to the documentation of this file.
1 /*
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SPIBase.h
8  *
9  * Created on: Mar 2, 2016
10  * Author: harry-boe
11  *
12  */
13 
14 #pragma once
15 
16 #include "SPISettings.h"
17 #include <stddef.h>
18 
25 /*
26  * @brief Base class/interface for SPI implementations
27  */
28 class SPIBase
29 {
30 public:
31  virtual ~SPIBase()
32  {
33  }
34 
38  virtual bool begin() = 0;
39 
43  virtual void end() = 0;
44 
48  void beginTransaction(SPISettings& settings)
49  {
50  prepare(settings);
51  }
52 
53  void beginTransaction(const SPISettings& settings)
54  {
55  SPISettings tmp{settings};
56  prepare(tmp);
57  }
58 
62  virtual void endTransaction()
63  {
64  }
65 
79  virtual uint8_t read8()
80  {
81  return transfer(0xff);
82  }
83 
101  uint8_t transfer(uint8_t val)
102  {
103  return transfer32(val, 8);
104  }
105 
112  {
113  return transfer32(val, 16);
114  }
115 
127  virtual uint32_t transfer32(uint32_t val, uint8_t bits = 32)
128  {
129  transfer(reinterpret_cast<uint8_t*>(&val), bits / 8);
130  return val;
131  }
132 
138  virtual void transfer(uint8_t* buffer, size_t size) = 0;
139 
149 
150 protected:
155  virtual void prepare(SPISettings& settings) = 0;
156 };
157 
Definition: SPISettings.h:66
virtual uint32_t transfer32(uint32_t val, uint8_t bits=32)
Send/receive a word of variable size.
Definition: SPIBase.h:127
virtual uint8_t read8()
Read one byte from SPI without setting up registers.
Definition: SPIBase.h:79
void beginTransaction(SPISettings &settings)
Initialize the SPI bus using the defined SPISettings.
Definition: SPIBase.h:48
virtual bool begin()=0
Initialize the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low...
virtual ~SPIBase()
Definition: SPIBase.h:31
virtual void endTransaction()
Stop using the SPI bus. Normally this is called after de-asserting the chip select, to allow other libraries to use the SPI bus.
Definition: SPIBase.h:62
virtual void end()=0
Disable the SPI bus (leaving pin modes unchanged).
Definition: SPIBase.h:28
SPISettings SPIDefaultSettings
Default settings used by the SPI bus until reset by beginTransaction(SPISettings) ...
Definition: SPIBase.h:148
void beginTransaction(const SPISettings &settings)
Definition: SPIBase.h:53
uint16_t transfer16(uint16_t val)
Send/receive one 16-bit word of data.
Definition: SPIBase.h:111
uint8_t transfer(uint8_t val)
Send/receive one bytes of data.
Definition: SPIBase.h:101
virtual void prepare(SPISettings &settings)=0
Prepare/configure with settings.