making SPI CS optional (#988)

* making SPI CS optional

* CS pin should be declared as optional or required in CONFIG_SCHEMA

* changed SPI_DEVICE_SCHEMA to a function, like i2c

* added spi_device_schema() to pcd8544, lint fixes

* updated max31856 with new spi_device_schema()

* cleanup imports

Co-authored-by: Ilya Goldberg <iggie@mac.com>
This commit is contained in:
igg
2020-06-10 13:03:11 -07:00
committed by GitHub
parent bab562dc3a
commit 821c1a8bbd
17 changed files with 67 additions and 43 deletions
+16 -6
View File
@@ -34,15 +34,25 @@ def to_code(config):
cg.add(var.set_mosi(mosi))
SPI_DEVICE_SCHEMA = cv.Schema({
cv.GenerateID(CONF_SPI_ID): cv.use_id(SPIComponent),
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
})
def spi_device_schema(CS_PIN_required=False):
"""Create a schema for an SPI device.
:param CS_PIN_required: If true, make the CS_PIN required in the config.
:return: The SPI device schema, `extend` this in your config schema.
"""
schema = {
cv.GenerateID(CONF_SPI_ID): cv.use_id(SPIComponent),
}
if CS_PIN_required:
schema[cv.Required(CONF_CS_PIN)] = pins.gpio_output_pin_schema
else:
schema[cv.Optional(CONF_CS_PIN)] = pins.gpio_output_pin_schema
return cv.Schema(schema)
@coroutine
def register_spi_device(var, config):
parent = yield cg.get_variable(config[CONF_SPI_ID])
cg.add(var.set_spi_parent(parent))
pin = yield cg.gpio_pin_expression(config[CONF_CS_PIN])
cg.add(var.set_cs_pin(pin))
if CONF_CS_PIN in config:
pin = yield cg.gpio_pin_expression(config[CONF_CS_PIN])
cg.add(var.set_cs_pin(pin))
+5 -3
View File
@@ -12,9 +12,11 @@ void ICACHE_RAM_ATTR HOT SPIComponent::disable() {
if (this->hw_spi_ != nullptr) {
this->hw_spi_->endTransaction();
}
ESP_LOGVV(TAG, "Disabling SPI Chip on pin %u...", this->active_cs_->get_pin());
this->active_cs_->digital_write(true);
this->active_cs_ = nullptr;
if (this->active_cs_) {
ESP_LOGVV(TAG, "Disabling SPI Chip on pin %u...", this->active_cs_->get_pin());
this->active_cs_->digital_write(true);
this->active_cs_ = nullptr;
}
}
void SPIComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up SPI bus...");
+17 -13
View File
@@ -127,19 +127,21 @@ class SPIComponent : public Component {
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, uint32_t DATA_RATE>
void enable(GPIOPin *cs) {
SPIComponent::debug_enable(cs->get_pin());
if (cs) {
SPIComponent::debug_enable(cs->get_pin());
if (this->hw_spi_ != nullptr) {
uint8_t data_mode = (uint8_t(CLOCK_POLARITY) << 1) | uint8_t(CLOCK_PHASE);
SPISettings settings(DATA_RATE, BIT_ORDER, data_mode);
this->hw_spi_->beginTransaction(settings);
} else {
this->clk_->digital_write(CLOCK_POLARITY);
this->wait_cycle_ = uint32_t(F_CPU) / DATA_RATE / 2ULL;
if (this->hw_spi_ != nullptr) {
uint8_t data_mode = (uint8_t(CLOCK_POLARITY) << 1) | uint8_t(CLOCK_PHASE);
SPISettings settings(DATA_RATE, BIT_ORDER, data_mode);
this->hw_spi_->beginTransaction(settings);
} else {
this->clk_->digital_write(CLOCK_POLARITY);
this->wait_cycle_ = uint32_t(F_CPU) / DATA_RATE / 2ULL;
}
this->active_cs_ = cs;
this->active_cs_->digital_write(false);
}
this->active_cs_ = cs;
this->active_cs_->digital_write(false);
}
void disable();
@@ -174,8 +176,10 @@ class SPIDevice {
void set_cs_pin(GPIOPin *cs) { cs_ = cs; }
void spi_setup() {
this->cs_->setup();
this->cs_->digital_write(true);
if (this->cs_) {
this->cs_->setup();
this->cs_->digital_write(true);
}
}
void enable() { this->parent_->template enable<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, DATA_RATE>(this->cs_); }