SPISoft.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  * SPISoft.h
8  *
9  * Author: ADiea
10  * Project: Sming for ESP8266
11  * License: MIT
12  * Date: 15.07.2015
13  * Descr: Implement software SPI for HW configs other than hardware SPI pins(GPIO 12,13,14)
14  *
15  * @author mikee47 January 2022
16  *
17  * Rewritten to implement bit ordering, modes, delays and transactions.
18  *
19  ****/
20 
21 #pragma once
22 
23 #include "SPIBase.h"
24 
36 class SPISoft : public SPIBase
37 {
38 public:
47  SPISoft();
48 
54  SPISoft(uint8_t miso, uint8_t mosi, uint8_t sck, uint8_t delay = 0) : SPISoft({sck, miso, mosi}, delay)
55  {
56  }
57 
61  SPISoft(const SpiPins& pins, uint8_t delay = 0) : SPIBase(pins), m_delay(delay)
62  {
63  }
64 
69 
73  {
74  this->mPins = pins;
75  return true;
76  }
77 
78  bool begin() override;
79 
80  void end() override
81  {
82  }
83 
84  void endTransaction() override;
85 
86  using SPIBase::transfer;
87  uint32_t transfer32(uint32_t val, uint8_t bits = 32) override;
88  void transfer(uint8_t* buffer, size_t size) override;
89 
100  {
101  m_delay = delay;
102  }
103 
104  bool loopback(bool enable) override
105  {
106  (void)enable;
107  return false;
108  }
109 
110 protected:
111  void prepare(SPISettings& settings) override;
112 
113 private:
114  uint8_t transferByteLSB(uint8_t word);
115  uint8_t transferByteMSB(uint8_t word);
116  uint32_t transferWordLSB(uint32_t word, uint8_t bits);
117  uint32_t transferWordMSB(uint32_t word, uint8_t bits);
118 
119  uint8_t m_delay{0};
120  SpiMode dataMode{SPI_MODE0};
121  uint8_t cpol{0};
122  uint8_t cksample{0};
123  bool lsbFirst{false};
124 };
uint8_t transfer(uint8_t val)
Send/receive one byte of data.
Definition: SPIBase.h:143
const SpiPins & pins
Definition: SPIBase.h:199
void transfer(uint8_t *buffer, size_t size) override
Send/receive a variable-length block of data.
SPI pin connections.
Definition: SPIBase.h:36
uint32_t transfer32(uint32_t val, uint8_t bits=32) override
Send/receive a word of variable size.
@ SPI_MODE0
Definition: SPISettings.h:44
bool setup(SpiPins pins)
Definition: SPISoft.h:72
void end() override
Disable the SPI bus (leaving pin modes unchanged).
Definition: SPISoft.h:80
SPISoft()
Default constructor uses same pins as hardware SPI.
Definition: SPISettings.h:73
SpiPins mPins
Definition: SPIBase.h:224
void endTransaction() override
Stop using the SPI bus. Normally this is called after de-asserting the chip select,...
void prepare(SPISettings &settings) override
Prepare/configure with settings.
void delay(uint32_t milliseconds)
Pause execution.
SPISoft(const SpiPins &pins, uint8_t delay=0)
Specify pins plus optional delay.
Definition: SPISoft.h:61
SPISoft(uint8_t miso, uint8_t mosi, uint8_t sck, uint8_t delay=0)
Specify pins to use plus optional delay.
Definition: SPISoft.h:54
Software-based SPI master.
Definition: SPISoft.h:36
SpiMode
Definition: SPISettings.h:32
bool loopback(bool enable) override
For testing, tie MISO <-> MOSI internally.
Definition: SPISoft.h:104
bool begin() override
Initialize the SPI bus by setting SCK and MOSI to outputs, pulling SCK and MOSI low.
void setDelay(uint8_t delay)
Set delay factor for the SCK signal. Impacts SPI speed.
Definition: SPISoft.h:99
void enable(Handler &commandHandler, HardwareSerial &serial)
Definition: SPIBase.h:50