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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 67 additions and 43 deletions

View file

@ -10,8 +10,8 @@ as3935_spi_ns = cg.esphome_ns.namespace('as3935_spi')
SPIAS3935 = as3935_spi_ns.class_('SPIAS3935Component', as3935.AS3935, spi.SPIDevice)
CONFIG_SCHEMA = cv.All(as3935.AS3935_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SPIAS3935)
}).extend(cv.COMPONENT_SCHEMA).extend(spi.SPI_DEVICE_SCHEMA))
cv.GenerateID(): cv.declare_id(SPIAS3935),
}).extend(cv.COMPONENT_SCHEMA).extend(spi.spi_device_schema(CS_PIN_required=True)))
def to_code(config):

View file

@ -55,7 +55,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.Required(CONF_LINE_FREQUENCY): cv.enum(LINE_FREQS, upper=True),
cv.Optional(CONF_CURRENT_PHASES, default='3'): cv.enum(CURRENT_PHASES, upper=True),
cv.Optional(CONF_GAIN_PGA, default='2X'): cv.enum(PGA_GAINS, upper=True),
}).extend(cv.polling_component_schema('60s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('60s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -11,7 +11,7 @@ CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({
cv.GenerateID(): cv.declare_id(MAX31855Sensor),
cv.Optional(CONF_REFERENCE_TEMPERATURE):
sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 2),
}).extend(cv.polling_component_schema('60s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('60s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -16,7 +16,7 @@ FILTER = {
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({
cv.GenerateID(): cv.declare_id(MAX31856Sensor),
cv.Optional(CONF_MAINS_FILTER, default='60HZ'): cv.enum(FILTER, upper=True, space=''),
}).extend(cv.polling_component_schema('60s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('60s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -20,7 +20,7 @@ CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 2).extend({
cv.Required(CONF_RTD_NOMINAL_RESISTANCE): cv.All(cv.resistance, cv.Range(min=100, max=1000)),
cv.Optional(CONF_MAINS_FILTER, default='60HZ'): cv.enum(FILTER, upper=True, space=''),
cv.Optional(CONF_RTD_WIRES, default=4): cv.int_range(min=2, max=4),
}).extend(cv.polling_component_schema('60s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('60s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -9,7 +9,7 @@ MAX6675Sensor = max6675_ns.class_('MAX6675Sensor', sensor.Sensor, cg.PollingComp
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({
cv.GenerateID(): cv.declare_id(MAX6675Sensor),
}).extend(cv.polling_component_schema('60s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('60s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -14,7 +14,7 @@ CONFIG_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend({
cv.Optional(CONF_NUM_CHIPS, default=1): cv.int_range(min=1, max=255),
cv.Optional(CONF_INTENSITY, default=15): cv.int_range(min=0, max=15),
}).extend(cv.polling_component_schema('1s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -17,7 +17,7 @@ CONFIG_SCHEMA = cv.All(display.FULL_DISPLAY_SCHEMA.extend({
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_RESET_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema, # CE
}).extend(cv.polling_component_schema('1s')).extend(spi.SPI_DEVICE_SCHEMA),
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema()),
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))

View file

@ -17,7 +17,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.Optional(CONF_ON_TAG): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PN532Trigger),
}),
}).extend(cv.polling_component_schema('1s')).extend(spi.SPI_DEVICE_SCHEMA)
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema())
def to_code(config):

View file

@ -31,7 +31,8 @@ void PN532::setup() {
// (this may time out, but that's ok)
// 3. Send SAM config command with normal mode without waiting for ready bit (IRQ not initialized yet)
// 4. Probably optional, send SAM config again, this time checking ACK and return value
this->cs_->digital_write(false);
if (this->cs_)
this->cs_->digital_write(false);
delay(10);
// send dummy firmware version command to get synced up

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))

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...");

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_); }

View file

@ -13,7 +13,7 @@ SPISSD1306 = ssd1306_spi.class_('SPISSD1306', ssd1306_base.SSD1306, spi.SPIDevic
CONFIG_SCHEMA = cv.All(ssd1306_base.SSD1306_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SPISSD1306),
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
}).extend(cv.COMPONENT_SCHEMA).extend(spi.SPI_DEVICE_SCHEMA),
}).extend(cv.COMPONENT_SCHEMA).extend(spi.spi_device_schema()),
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))

View file

@ -13,7 +13,7 @@ SPISSD1325 = ssd1325_spi.class_('SPISSD1325', ssd1325_base.SSD1325, spi.SPIDevic
CONFIG_SCHEMA = cv.All(ssd1325_base.SSD1325_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SPISSD1325),
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
}).extend(cv.COMPONENT_SCHEMA).extend(spi.SPI_DEVICE_SCHEMA),
}).extend(cv.COMPONENT_SCHEMA).extend(spi.spi_device_schema()),
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))

View file

@ -11,7 +11,8 @@ void SPISSD1325::setup() {
ESP_LOGCONFIG(TAG, "Setting up SPI SSD1325...");
this->spi_setup();
this->dc_pin_->setup(); // OUTPUT
this->cs_->setup(); // OUTPUT
if (this->cs_)
this->cs_->setup(); // OUTPUT
this->init_reset_();
delay(500); // NOLINT
@ -27,19 +28,24 @@ void SPISSD1325::dump_config() {
LOG_UPDATE_INTERVAL(this);
}
void SPISSD1325::command(uint8_t value) {
this->cs_->digital_write(true);
if (this->cs_)
this->cs_->digital_write(true);
this->dc_pin_->digital_write(false);
delay(1);
this->enable();
this->cs_->digital_write(false);
if (this->cs_)
this->cs_->digital_write(false);
this->write_byte(value);
this->cs_->digital_write(true);
if (this->cs_)
this->cs_->digital_write(true);
this->disable();
}
void HOT SPISSD1325::write_display_data() {
this->cs_->digital_write(true);
if (this->cs_)
this->cs_->digital_write(true);
this->dc_pin_->digital_write(true);
this->cs_->digital_write(false);
if (this->cs_)
this->cs_->digital_write(false);
delay(1);
this->enable();
for (uint16_t x = 0; x < this->get_width_internal(); x += 2) {
@ -56,7 +62,8 @@ void HOT SPISSD1325::write_display_data() {
}
}
}
this->cs_->digital_write(true);
if (this->cs_)
this->cs_->digital_write(true);
this->disable();
}

View file

@ -50,7 +50,7 @@ CONFIG_SCHEMA = cv.All(display.FULL_DISPLAY_SCHEMA.extend({
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_BUSY_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_FULL_UPDATE_EVERY): cv.uint32_t,
}).extend(cv.polling_component_schema('1s')).extend(spi.SPI_DEVICE_SCHEMA),
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema()),
validate_full_update_every_only_type_a,
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))