8 #define USE_SPI_ARDUINO_BACKEND 11 #ifdef USE_SPI_ARDUINO_BACKEND 79 void setup()
override;
83 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE> uint8_t
read_byte() {
84 #ifdef USE_SPI_ARDUINO_BACKEND 86 return this->
hw_spi_->transfer(0x00);
88 #endif // USE_SPI_ARDUINO_BACKEND 89 return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, false>(0x00);
92 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
94 #ifdef USE_SPI_ARDUINO_BACKEND 96 this->
hw_spi_->transfer(data, length);
99 #endif // USE_SPI_ARDUINO_BACKEND 100 for (
size_t i = 0; i < length; i++) {
101 data[i] = this->read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>();
105 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
107 #ifdef USE_SPI_ARDUINO_BACKEND 108 if (this->
hw_spi_ !=
nullptr) {
116 #endif // USE_SPI_ARDUINO_BACKEND 117 this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, false, true>(data);
120 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
122 #ifdef USE_SPI_ARDUINO_BACKEND 123 if (this->
hw_spi_ !=
nullptr) {
125 this->
hw_spi_->transfer16(data);
131 #endif // USE_SPI_ARDUINO_BACKEND 133 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data >> 8);
134 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
137 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
139 #ifdef USE_SPI_ARDUINO_BACKEND 140 if (this->
hw_spi_ !=
nullptr) {
141 for (
size_t i = 0; i < length; i++) {
143 this->
hw_spi_->transfer16(data[i]);
145 this->
hw_spi_->write16(data[i]);
150 #endif // USE_SPI_ARDUINO_BACKEND 151 for (
size_t i = 0; i < length; i++) {
152 this->write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
156 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
158 #ifdef USE_SPI_ARDUINO_BACKEND 159 if (this->
hw_spi_ !=
nullptr) {
160 auto *data_c =
const_cast<uint8_t *
>(data);
162 this->
hw_spi_->transfer(data_c, length);
164 this->
hw_spi_->writeBytes(data_c, length);
168 #endif // USE_SPI_ARDUINO_BACKEND 169 for (
size_t i = 0; i < length; i++) {
170 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
174 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
176 if (this->
miso_ !=
nullptr) {
177 #ifdef USE_SPI_ARDUINO_BACKEND 178 if (this->
hw_spi_ !=
nullptr) {
179 return this->
hw_spi_->transfer(data);
181 #endif // USE_SPI_ARDUINO_BACKEND 182 return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, true>(data);
183 #ifdef USE_SPI_ARDUINO_BACKEND 185 #endif // USE_SPI_ARDUINO_BACKEND 187 this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
191 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
193 #ifdef USE_SPI_ARDUINO_BACKEND 194 if (this->
hw_spi_ !=
nullptr) {
195 if (this->
miso_ !=
nullptr) {
196 this->
hw_spi_->transfer(data, length);
199 this->
hw_spi_->transfer(data, length);
201 this->
hw_spi_->writeBytes(data, length);
206 #endif // USE_SPI_ARDUINO_BACKEND 208 if (this->
miso_ !=
nullptr) {
209 for (
size_t i = 0; i < length; i++) {
210 data[i] = this->transfer_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
213 this->write_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
217 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, u
int32_t DATA_RATE>
219 #ifdef USE_SPI_ARDUINO_BACKEND 220 if (this->
hw_spi_ !=
nullptr) {
221 uint8_t data_mode = SPI_MODE0;
222 if (!CLOCK_POLARITY && CLOCK_PHASE) {
223 data_mode = SPI_MODE1;
224 }
else if (CLOCK_POLARITY && !CLOCK_PHASE) {
225 data_mode = SPI_MODE2;
226 }
else if (CLOCK_POLARITY && CLOCK_PHASE) {
227 data_mode = SPI_MODE3;
230 SPISettings settings(DATA_RATE, static_cast<BitOrder>(BIT_ORDER), data_mode);
232 SPISettings settings(DATA_RATE, BIT_ORDER, data_mode);
234 this->
hw_spi_->beginTransaction(settings);
236 #endif // USE_SPI_ARDUINO_BACKEND 239 this->
wait_cycle_ = uint32_t(cpu_freq_hz) / DATA_RATE / 2ULL;
240 #ifdef USE_SPI_ARDUINO_BACKEND 242 #endif // USE_SPI_ARDUINO_BACKEND 257 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE,
bool READ,
bool WRITE>
265 #ifdef USE_SPI_ARDUINO_BACKEND 267 #endif // USE_SPI_ARDUINO_BACKEND 271 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, SPIDataRate DATA_RATE>
283 this->cs_->digital_write(
true);
287 void enable() { this->parent_->template enable<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, DATA_RATE>(this->cs_); }
291 uint8_t
read_byte() {
return this->parent_->template read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(); }
294 return this->parent_->template read_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
298 std::array<uint8_t, N> data;
304 return this->parent_->template write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
308 return this->parent_->template write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
312 this->parent_->template write_array16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
316 this->parent_->template write_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
324 return this->parent_->template transfer_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
328 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)
void set_force_sw(bool force_sw)
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