25 #include "driver/spi_master.h" 175 : bit_order_(bit_order), data_rate_(data_rate), mode_(mode), cs_pin_(cs_pin) {
176 if (this->cs_pin_ ==
nullptr)
178 this->cs_pin_->
setup();
179 this->cs_pin_->digital_write(
true);
191 virtual uint8_t transfer(uint8_t data) = 0;
197 for (
size_t i = 0; i !=
length; i++)
198 rxbuf[i] = this->transfer(txbuf[i]);
206 virtual void write(uint16_t data,
size_t num_bits) {
207 esph_log_e(
"spi_device",
"variable length write not implemented");
211 const uint8_t *data,
size_t length, uint8_t bus_width) {
212 esph_log_e(
"spi_device",
"write_cmd_addr_data not implemented");
218 buffer = (data >> 8) | (data << 8);
219 this->write_array(reinterpret_cast<const uint8_t *>(&buffer), 2);
221 this->write_array(reinterpret_cast<const uint8_t *>(&data), 2);
226 for (
size_t i = 0; i !=
length; i++) {
227 this->write16(data[i]);
233 for (
size_t i = 0; i !=
length; i++)
234 this->transfer(ptr[i]);
239 for (
size_t i = 0; i !=
length; i++)
240 ptr[i] = this->transfer(0);
244 virtual bool is_ready();
248 uint32_t data_rate_{1000000};
261 :
SPIDelegate(clock, bit_order, mode, cs_pin), clk_pin_(clk_pin), sdo_pin_(sdo_pin), sdi_pin_(sdi_pin) {
268 uint8_t transfer(uint8_t data)
override;
270 void write(uint16_t data,
size_t num_bits)
override;
272 void write16(uint16_t data)
override { this->write(data, 16); };
278 uint32_t last_transition_{0};
286 this->last_transition_ += this->wait_cycle_;
288 uint16_t transfer_(uint16_t data,
size_t num_bits);
298 return new SPIDelegateBitBash(data_rate, bit_order, mode, cs_pin, this->clk_pin_, this->sdo_pin_, this->sdi_pin_);
301 virtual bool is_hw() {
return false; }
315 void unregister_device(
SPIClient *device);
322 void set_data_pins(std::vector<uint8_t> pins) { this->data_pins_ = std::move(pins); }
325 this->interface_ = interface;
326 this->using_hw_ =
true;
333 void setup()
override;
334 void dump_config()
override;
340 std::vector<uint8_t> data_pins_{};
343 bool using_hw_{
false};
344 const char *interface_name_{
nullptr};
349 const std::vector<uint8_t> &data_pins);
359 : bit_order_(bit_order), mode_(mode), data_rate_(data_rate) {}
362 esph_log_d(
"spi_device",
"mode %u, data_rate %ukHz", (
unsigned) this->mode_, (
unsigned) (this->data_rate_ / 1000));
363 this->delegate_ = this->parent_->register_device(
this, this->mode_, this->bit_order_, this->data_rate_, this->cs_);
367 this->parent_->unregister_device(
this);
368 this->delegate_ =
nullptr;
376 uint32_t data_rate_{1000000};
390 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, SPIDataRate DATA_RATE>
396 this->set_spi_parent(parent);
397 this->set_cs_pin(cs_pin);
414 uint8_t
read_byte() {
return this->delegate_->transfer(0); }
416 void read_array(uint8_t *data,
size_t length) {
return this->delegate_->read_array(data, length); }
423 void write(uint16_t data,
size_t num_bits) { this->delegate_->write(data, num_bits); };
436 size_t length, uint8_t bus_width = 1) {
437 this->delegate_->write_cmd_addr_data(cmd_bits, cmd, addr_bits, address, data, length, bus_width);
440 void write_byte(uint8_t data) { this->delegate_->write_array(&data, 1); }
449 uint8_t
transfer_byte(uint8_t data) {
return this->delegate_->transfer(data); }
463 void enable() { this->delegate_->begin_transaction(); }
465 void disable() { this->delegate_->end_transaction(); }
467 void write_array(
const uint8_t *data,
size_t length) { this->delegate_->write_array(data, length); }
void write_array16(const uint16_t *data, size_t length)
Write an array of data as 16 bit values, byte-swapping if required.
virtual void spi_teardown()
void transfer_array(uint8_t *data, size_t length)
Write the array data, replace with received data.
SPIClassRP2040 * SPIInterface
virtual void read_array(uint8_t *ptr, size_t length)
SPIClockPolarity clock_polarity_
SPIDataRate
The SPI clock signal frequency, which determines the transfer bit rate/second.
virtual void transfer(uint8_t *ptr, size_t length)
void spi_setup() override
The data is sampled on a leading clock edge. (CPHA=0)
void set_mosi(GPIOPin *sdo)
std::string dump_summary() const override
The clock signal idles on HIGH.
A pin to replace those that don't exist.
static SPIMode get_mode(SPIClockPolarity polarity, SPIClockPhase phase)
std::map< SPIClient *, SPIDelegate * > devices_
void pin_mode(gpio::Flags flags) override
SPIDelegateBitBash(uint32_t clock, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin, GPIOPin *clk_pin, GPIOPin *sdo_pin, GPIOPin *sdi_pin)
The most significant bit is transmitted/received first.
virtual void write(uint16_t data, size_t num_bits)
write a variable length data item, up to 16 bits.
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 spi_teardown() override
void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data, size_t length, uint8_t bus_width=1)
SPIClockPolarity
The SPI clock signal polarity,.
static int get_pin_no(GPIOPin *pin)
The SPIDevice is what components using the SPI will create.
bool digital_read() override
void write_array(const std::array< uint8_t, N > &data)
void read_array(uint8_t *data, size_t length)
const float BUS
For communication buses like i2c/spi.
The data is sampled on a trailing clock edge. (CPHA=1)
virtual bool is_internal()
void write(uint16_t data, size_t num_bits)
Write a single data item, up to 32 bits.
BedjetMode mode
BedJet operating mode.
virtual void transfer(const uint8_t *txbuf, uint8_t *rxbuf, size_t length)
SPIMode
Modes mapping to clock phase and polarity.
void set_mode(SPIMode mode)
uint32_t arch_get_cpu_freq_hz()
virtual void end_transaction()
void set_miso(GPIOPin *sdi)
Base class for SPIDevice, un-templated.
virtual void begin_transaction()
SPIClient(SPIBitOrder bit_order, SPIMode mode, uint32_t data_rate)
virtual void write16(uint16_t data)
void set_bit_order(SPIBitOrder order)
static SPIClockPolarity get_polarity(SPIMode mode)
virtual SPIDelegate * get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin)
void set_data_pins(std::vector< uint8_t > pins)
SPIClockPhase clock_phase_
An implementation of SPI that relies only on software toggling of pins.
void transfer_array(std::array< uint8_t, N > &data)
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
void set_interface(SPIInterface interface)
void write_array(const uint8_t *data, size_t length)
SPIDelegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin)
virtual void write_array(const uint8_t *ptr, size_t length)
Implementation of SPI Controller mode.
friend class SPIComponent
The least significant bit is transmitted/received first.
void set_spi_parent(SPIComponent *parent)
SPIClockPhase
The SPI clock signal phase.
SPIBus(GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi)
void set_interface_name(const char *name)
void write_byte16(uint16_t data)
Write 16 bit data.
void set_data_rate(uint32_t data_rate)
void digital_write(bool value) override
void set_clk(GPIOPin *clk)
static GPIOPin *const NULL_PIN
uint32_t arch_get_cpu_cycle_count()
void write16(uint16_t data) override
SPIDevice(SPIComponent *parent, GPIOPin *cs_pin)
virtual void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data, size_t length, uint8_t bus_width)
static SPIClockPhase get_phase(SPIMode mode)
virtual void write_array16(const uint16_t *data, size_t length)
float get_setup_priority() const override