SPISoft.h
Go to the documentation of this file.
1 /*
2 Author: ADiea
3 Project: Sming for ESP8266
4 License: MIT
5 Date: 15.07.2015
6 Descr: Implement software SPI for HW configs other than hardware SPI pins(GPIO 12,13,14)
7 */
8 #pragma once
9 
10 #include "SPIBase.h"
11 #include "SPISettings.h"
12 
13 class SPISoft : public SPIBase
14 {
15 public:
16  SPISoft(uint16_t miso, uint16_t mosi, uint16_t sck, uint8_t delay)
17  : mMISO(miso), mMOSI(mosi), mCLK(sck), m_delay(delay)
18  {
19  }
20 
21  /*
22  * begin(): Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
23  */
24  void begin() override; //setup pins
25 
26  /*
27  * end(): Disables the SPI bus (leaving pin modes unchanged).
28  */
29  void end() override
30  {
31  }
32 
33  /*
34  * beginTransaction(): Initializes the SPI bus using the defined SPISettings.
35  */
36  void beginTransaction(SPISettings mySettings) override
37  {
38  }
39 
40  /*
41  * 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.
42  */
43  void endTransaction() override
44  {
45  }
46 
47  /*
48  * transfer(), transfer16()
49  *
50  * SPI transfer is based on a simultaneous send and receive: the received data is returned in receivedVal (or receivedVal16). In case of buffer transfers the received data is stored in the buffer in-place (the old data is replaced with the data received).
51  *
52  * receivedVal = SPI.transfer(val)
53  * receivedVal16 = SPI.transfer16(val16)
54  * SPI.transfer(buffer, size)
55  */
56  void transfer(uint8_t* buffer, size_t size) override;
57 
58  unsigned char transfer(unsigned char val) override
59  {
60  transfer(&val, 1);
61  return val;
62  }
63 
64  unsigned short transfer16(unsigned short val) override
65  {
66  transfer(reinterpret_cast<uint8_t*>(&val), 2);
67  return val;
68  }
69 
73  void setDelay(uint8_t dly)
74  {
75  m_delay = dly;
76  }
77 
78 private:
79  SPISettings mSPISettings;
80  uint16_t mMISO, mMOSI, mCLK;
81  uint8_t m_delay;
82 };
Definition: SPISettings.h:33
void beginTransaction(SPISettings mySettings) override
beginTransaction(): Initializes the SPI bus using the defined SPISettings.
Definition: SPISoft.h:36
void begin() override
begin(): Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low...
void transfer(uint8_t *buffer, size_t size) override
void setDelay(uint8_t dly)
Set microsecond delay for the SCK signal. Impacts SPI speed.
Definition: SPISoft.h:73
void delay(uint32_t milliseconds)
Pause execution.
Definition: SPISoft.h:13
SPISoft(uint16_t miso, uint16_t mosi, uint16_t sck, uint8_t delay)
Definition: SPISoft.h:16
unsigned short transfer16(unsigned short val) override
Definition: SPISoft.h:64
unsigned char transfer(unsigned char val) override
transfer(), transfer16()
Definition: SPISoft.h:58
Definition: SPIBase.h:25
void endTransaction() override
endTransaction(): Stop using the SPI bus. Normally this is called after de-asserting the chip select...
Definition: SPISoft.h:43
void end() override
end(): Disables the SPI bus (leaving pin modes unchanged).
Definition: SPISoft.h:29