SPI.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  * SPI.h
8  *
9  * Created on: Mar 2, 2016
10  * Author: harry-boe
11  *
12  */
13 
14 #pragma once
15 
16 #include "SPIBase.h"
17 #include "SPISettings.h"
18 #include <spi_arch.h>
19 
29 class SPIClass : public SPIBase
30 {
31 public:
32  SPIClass();
33  SPIClass(const SPIClass&) = delete;
34  SPIClass& operator=(const SPIClass&) = delete;
35 
42  bool setup(SpiBus id, SpiPins pins = {});
43 
45  {
46  return setup(SpiBus::DEFAULT, pins);
47  }
48 
49  bool begin() override;
50  void end() override;
51 
52  uint8_t read8() override;
53  uint32_t transfer32(uint32_t val, uint8_t bits = 32) override;
54 
55  using SPIBase::transfer;
56  void transfer(uint8_t* buffer, size_t numberBytes) override;
57 
58  bool loopback(bool enable) override;
59 
60 #ifdef ARCH_HOST
61 
67  using IoCallback = void (*)(uint16_t c, uint8_t bits, bool read);
68 
75  void setDebugIoCallback(IoCallback callback);
76 #endif
77 
78 protected:
79  void prepare(SPISettings& settings) override;
80 
81 private:
82 #ifndef ARCH_ESP8266
83  SpiBus busId{SpiBus::DEFAULT};
84 #endif
85 #if defined(ARCH_RP2040) || defined(ARCH_HOST)
86  uint16_t cr0val{0};
87 #endif
88  bool lsbFirst{false};
89 };
90 
92 extern SPIClass SPI;
93 
uint8_t transfer(uint8_t val)
Send/receive one byte of data.
Definition: SPIBase.h:143
const SpiPins & pins
Definition: SPIBase.h:199
void end() override
Disable the SPI bus (leaving pin modes unchanged).
bool begin() override
Initialize the SPI bus by setting SCK and MOSI to outputs, pulling SCK and MOSI low.
Hardware SPI class.
Definition: SPI.h:29
SPI pin connections.
Definition: SPIBase.h:36
void(*)(uint16_t c, uint8_t bits, bool read) IoCallback
Used for testing purposes only.
Definition: SPI.h:67
Timer2Clock::Ticks< uint32_t > read()
Get elapsed watchdog time since last reset.
Definition: SPISettings.h:73
SPIClass SPI
Global instance of SPI class.
uint8_t read8() override
Read one byte from SPI without setting up registers.
void transfer(uint8_t *buffer, size_t numberBytes) override
Send/receive a variable-length block of data.
bool setup(SpiPins pins)
Definition: SPI.h:44
SPIClass & operator=(const SPIClass &)=delete
bool loopback(bool enable) override
For testing, tie MISO <-> MOSI internally.
void enable(Handler &commandHandler, HardwareSerial &serial)
SpiBus
Identifies bus selection.
Definition: Esp32/spi_arch.h:23
void setDebugIoCallback(IoCallback callback)
Used for testing purposes only Must be called after begin().
uint32_t transfer32(uint32_t val, uint8_t bits=32) override
Send/receive a word of variable size.
Definition: SPIBase.h:50
bool setup(SpiBus id, SpiPins pins={})
Alternative to defining bus and pin set in constructor. Use this method to change global SPI instance...
void prepare(SPISettings &settings) override
Prepare/configure with settings.