Files
esphome-dev/esphome/components/spi/spi.cpp
T
Otto Winter 6682c43dfa 🏗 Merge C++ into python codebase (#504)
## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-17 12:06:00 +02:00

105 lines
2.8 KiB
C++

#include "spi.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "esphome/core/application.h"
namespace esphome {
namespace spi {
static const char *TAG = "spi";
void ICACHE_RAM_ATTR HOT SPIComponent::write_byte(uint8_t data) {
uint8_t send_bits = data;
if (this->msb_first_)
send_bits = reverse_bits_8(data);
this->clk_->digital_write(true);
if (!this->high_speed_)
delayMicroseconds(5);
for (size_t i = 0; i < 8; i++) {
if (!this->high_speed_)
delayMicroseconds(5);
this->clk_->digital_write(false);
// sampling on leading edge
this->mosi_->digital_write(send_bits & (1 << i));
if (!this->high_speed_)
delayMicroseconds(5);
this->clk_->digital_write(true);
}
ESP_LOGVV(TAG, " Wrote 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(data), data);
}
uint8_t ICACHE_RAM_ATTR HOT SPIComponent::read_byte() {
this->clk_->digital_write(true);
uint8_t data = 0;
for (size_t i = 0; i < 8; i++) {
if (!this->high_speed_)
delayMicroseconds(5);
data |= uint8_t(this->miso_->digital_read()) << i;
this->clk_->digital_write(false);
if (!this->high_speed_)
delayMicroseconds(5);
this->clk_->digital_write(true);
}
if (this->msb_first_) {
data = reverse_bits_8(data);
}
ESP_LOGVV(TAG, " Received 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(data), data);
return data;
}
void ICACHE_RAM_ATTR HOT SPIComponent::read_array(uint8_t *data, size_t length) {
for (size_t i = 0; i < length; i++)
data[i] = this->read_byte();
}
void ICACHE_RAM_ATTR HOT SPIComponent::write_array(uint8_t *data, size_t length) {
for (size_t i = 0; i < length; i++) {
App.feed_wdt();
this->write_byte(data[i]);
}
}
void ICACHE_RAM_ATTR HOT SPIComponent::enable(GPIOPin *cs, bool msb_first, bool high_speed) {
ESP_LOGVV(TAG, "Enabling SPI Chip on pin %u...", cs->get_pin());
cs->digital_write(false);
this->active_cs_ = cs;
this->msb_first_ = msb_first;
this->high_speed_ = high_speed;
}
void ICACHE_RAM_ATTR HOT SPIComponent::disable() {
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...");
this->clk_->setup();
this->clk_->digital_write(true);
if (this->miso_ != nullptr) {
this->miso_->setup();
}
if (this->mosi_ != nullptr) {
this->mosi_->setup();
this->mosi_->digital_write(false);
}
}
void SPIComponent::dump_config() {
ESP_LOGCONFIG(TAG, "SPI bus:");
LOG_PIN(" CLK Pin: ", this->clk_);
LOG_PIN(" MISO Pin: ", this->miso_);
LOG_PIN(" MOSI Pin: ", this->mosi_);
}
float SPIComponent::get_setup_priority() const { return setup_priority::BUS; }
} // namespace spi
} // namespace esphome