8 #define USE_SPI_ARDUINO_BACKEND 11 #ifdef USE_SPI_ARDUINO_BACKEND 78 void setup()
override;
82 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE> uint8_t
read_byte() {
83 #ifdef USE_SPI_ARDUINO_BACKEND 85 return this->
hw_spi_->transfer(0x00);
87 #endif // USE_SPI_ARDUINO_BACKEND 88 return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, false>(0x00);
91 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
93 #ifdef USE_SPI_ARDUINO_BACKEND 95 this->
hw_spi_->transfer(data, length);
98 #endif // USE_SPI_ARDUINO_BACKEND 99 for (
size_t i = 0; i < length; i++) {
100 data[i] = this->read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>();
104 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
106 #ifdef USE_SPI_ARDUINO_BACKEND 107 if (this->
hw_spi_ !=
nullptr) {
115 #endif // USE_SPI_ARDUINO_BACKEND 116 this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, false, true>(data);
119 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
121 #ifdef USE_SPI_ARDUINO_BACKEND 122 if (this->
hw_spi_ !=
nullptr) {
124 this->
hw_spi_->transfer16(data);
130 #endif // USE_SPI_ARDUINO_BACKEND 132 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data >> 8);
133 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
136 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
138 #ifdef USE_SPI_ARDUINO_BACKEND 139 if (this->
hw_spi_ !=
nullptr) {
140 for (
size_t i = 0; i < length; i++) {
142 this->
hw_spi_->transfer16(data[i]);
144 this->
hw_spi_->write16(data[i]);
149 #endif // USE_SPI_ARDUINO_BACKEND 150 for (
size_t i = 0; i < length; i++) {
151 this->write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
155 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
157 #ifdef USE_SPI_ARDUINO_BACKEND 158 if (this->
hw_spi_ !=
nullptr) {
159 auto *data_c =
const_cast<uint8_t *
>(data);
161 this->
hw_spi_->transfer(data_c, length);
163 this->
hw_spi_->writeBytes(data_c, length);
167 #endif // USE_SPI_ARDUINO_BACKEND 168 for (
size_t i = 0; i < length; i++) {
169 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
173 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
175 if (this->
miso_ !=
nullptr) {
176 #ifdef USE_SPI_ARDUINO_BACKEND 177 if (this->
hw_spi_ !=
nullptr) {
178 return this->
hw_spi_->transfer(data);
180 #endif // USE_SPI_ARDUINO_BACKEND 181 return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, true>(data);
182 #ifdef USE_SPI_ARDUINO_BACKEND 184 #endif // USE_SPI_ARDUINO_BACKEND 186 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
190 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
192 #ifdef USE_SPI_ARDUINO_BACKEND 193 if (this->
hw_spi_ !=
nullptr) {
194 if (this->
miso_ !=
nullptr) {
195 this->
hw_spi_->transfer(data, length);
198 this->
hw_spi_->transfer(data, length);
200 this->
hw_spi_->writeBytes(data, length);
205 #endif // USE_SPI_ARDUINO_BACKEND 207 if (this->
miso_ !=
nullptr) {
208 for (
size_t i = 0; i < length; i++) {
209 data[i] = this->transfer_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
212 this->write_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
216 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, u
int32_t DATA_RATE>
218 #ifdef USE_SPI_ARDUINO_BACKEND 219 if (this->
hw_spi_ !=
nullptr) {
220 uint8_t data_mode = SPI_MODE0;
221 if (!CLOCK_POLARITY && CLOCK_PHASE) {
222 data_mode = SPI_MODE1;
223 }
else if (CLOCK_POLARITY && !CLOCK_PHASE) {
224 data_mode = SPI_MODE2;
225 }
else if (CLOCK_POLARITY && CLOCK_PHASE) {
226 data_mode = SPI_MODE3;
229 SPISettings settings(DATA_RATE, static_cast<BitOrder>(BIT_ORDER), data_mode);
231 SPISettings settings(DATA_RATE, BIT_ORDER, data_mode);
233 this->
hw_spi_->beginTransaction(settings);
235 #endif // USE_SPI_ARDUINO_BACKEND 238 this->
wait_cycle_ = uint32_t(cpu_freq_hz) / DATA_RATE / 2ULL;
239 #ifdef USE_SPI_ARDUINO_BACKEND 241 #endif // USE_SPI_ARDUINO_BACKEND 256 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE,
bool READ,
bool WRITE>
263 #ifdef USE_SPI_ARDUINO_BACKEND 265 #endif // USE_SPI_ARDUINO_BACKEND 269 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, SPIDataRate DATA_RATE>
281 this->cs_->digital_write(
true);
285 void enable() { this->parent_->template enable<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, DATA_RATE>(this->cs_); }
289 uint8_t
read_byte() {
return this->parent_->template read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(); }
292 return this->parent_->template read_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
296 std::array<uint8_t, N> data;
302 return this->parent_->template write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
306 return this->parent_->template write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
310 this->parent_->template write_array16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
314 this->parent_->template write_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
322 return this->parent_->template transfer_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
326 this->parent_->template transfer_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
virtual void digital_write(bool value)=0
void write_array16(const uint16_t *data, size_t length)
void transfer_array(uint8_t *data, size_t length)
void cycle_clock_(bool value)
void transfer_array(uint8_t *data, size_t length)
void set_miso(GPIOPin *miso)
SPIDataRate
The SPI clock signal data rate.
std::array< uint8_t, N > read_array()
The data is sampled on a leading clock edge. (CPHA=0)
The clock signal idles on HIGH.
The most significant bit is transmitted/received first.
void set_cs_pin(GPIOPin *cs)
uint8_t transfer_byte(uint8_t data)
The clock signal idles on LOW.
void write_array(const std::vector< uint8_t > &data)
void write_byte(uint8_t data)
void read_array(uint8_t *data, size_t length)
SPIClockPolarity
The SPI clock signal polarity,.
void dump_config() override
void write_array(const std::array< uint8_t, N > &data)
void read_array(uint8_t *data, size_t length)
The data is sampled on a trailing clock edge. (CPHA=1)
void set_mosi(GPIOPin *mosi)
uint32_t arch_get_cpu_freq_hz()
SPIDevice(SPIComponent *parent, GPIOPin *cs)
void write_byte(uint8_t data)
void transfer_array(std::array< uint8_t, N > &data)
uint8_t transfer_(uint8_t data)
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
void write_array(const uint8_t *data, size_t length)
The least significant bit is transmitted/received first.
void set_spi_parent(SPIComponent *parent)
SPIClockPhase
The SPI clock signal phase.
void write_byte16(uint16_t data)
void write_byte16(const uint16_t data)
void write_array16(const uint16_t *data, size_t length)
void set_clk(GPIOPin *clk)
uint8_t transfer_byte(uint8_t data)
void write_array(const uint8_t *data, size_t length)
float get_setup_priority() const override