diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index 2039aae00f..f9e3abd008 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -8,11 +8,10 @@ from esphome.automation import maybe_simple_id from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority from esphome.components import sensor from esphome.py_compat import text_type, binary_type, char_to_byte -from esphome.const import CONF_ID, CONF_TRIGGER_ID, CONF_DATA +from esphome.const import CONF_ID, CONF_TRIGGER_ID, CONF_DATA, CONF_ON_MESSAGE IS_PLATFORM_COMPONENT = True -CONF_ON_RECEIVE = 'on_receive' CONF_CANBUS_ID = 'canbus_id' CONF_CAN_ID = 'can_id' CONF_SENDER_ID = 'sender_id' @@ -36,7 +35,7 @@ CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component) CanbusTrigger = canbus_ns.class_('CanbusTrigger', automation.Trigger.template(cg.std_string), cg.Component) -CanSpeed = canbus_ns.enum('CanSpeed') +CanSpeed = canbus_ns.enum('CAN_SPEED') CAN_SPEEDS = { '5KBPS': CanSpeed.CAN_5KBPS, @@ -61,10 +60,10 @@ CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(CanbusComponent), cv.Required(CONF_SENDER_ID): cv.int_range(min=0, max=255), cv.Optional(CONF_BIT_RATE, default='125KBPS'): cv.enum(CAN_SPEEDS, upper=True), - cv.Optional(CONF_ON_RECEIVE): automation.validate_automation({ - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger), - cv.GenerateID(CONF_CAN_ID): cv.int_range(min=1, max=4096), - }), + # cv.Optional(CONF_ON_MESSAGE): automation.validate_automation({ + # cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger), + # cv.GenerateID(CONF_CAN_ID): cv.int_range(min=1, max=4096), + # }), }).extend(cv.COMPONENT_SCHEMA) # Actions @@ -84,9 +83,13 @@ def setup_canbus_core_(var, config): cg.add(var.set_canbus_id(config[CONF_CANBUS_ID])) if CONF_SENDER_ID in config: cg.add(var.set_sender_id([config[CONF_SENDER_ID]])) - if CONF_DATA in config: - cg.add(var.set_can_data([config[CONF_DATA]])) - + if CONF_BIT_RATE in config: + bitrate = CAN_SPEEDS[config[CONF_BIT_RATE]] + cg.add(var.set_bitrate(bitrate)) + # for conf in config.get(CONF_ON_MESSAGE, []): + # trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_CAN_ID]) + # yield cg.register_component(trig, conf) + # yield automation.build_automation(trig, [(cg.uint32, 'x')], conf) @coroutine def register_canbus(var, config): @@ -105,7 +108,6 @@ def canbus_action_to_code(config, action_id, template_arg, args): data = config[CONF_DATA] if isinstance(data, binary_type): data = [char_to_byte(x) for x in data] - if cg.is_template(data): templ = yield cg.templatable(data, args, cg.std_vector.template(cg.uint8)) cg.add(var.set_data_template(templ)) diff --git a/esphome/components/canbus/canbus.cpp b/esphome/components/canbus/canbus.cpp index 31bbb81af1..ab4089e87c 100644 --- a/esphome/components/canbus/canbus.cpp +++ b/esphome/components/canbus/canbus.cpp @@ -31,31 +31,9 @@ void Canbus::send_data(uint32_t can_id, const std::vector data) { ESP_LOGD(TAG, "data[%d] = %02x", i, can_message.data[i]); } - // can_message.can_id = this->sender_id_; - // can_message.can_dlc = 8; - // can_message.data[0] = 0x00; - // can_message.data[1] = 0x01; - // can_message.data[2] = 0x02; - // can_message.data[3] = 0x03; - // can_message.data[4] = 0x04; - // can_message.data[5] = 0x05; - // can_message.data[6] = 0x06; - // can_message.data[7] = 0x07; - //this->dump_frame_(&can_message); - this->send_message_(&can_message); } -void Canbus::dump_frame_(const struct can_frame *data_frame) { - //ESP_LOGD(TAG, "dump_frame"); - //ESP_LOGD(TAG, "canid %d", frame.can_id); - //ESP_LOGD(TAG, "can_id %02x", data_frame->can_id); - // for (int i = 0; i < 8; i++) { - // data_frame->data[i]; - // } - return; -} - void Canbus::loop() { // check harware inputbuffer and process to esphome outputs } diff --git a/esphome/components/canbus/canbus.h b/esphome/components/canbus/canbus.h index fb3e9ce6b6..1511009a5c 100644 --- a/esphome/components/canbus/canbus.h +++ b/esphome/components/canbus/canbus.h @@ -3,7 +3,6 @@ #include "esphome/core/component.h" #include "esphome/core/automation.h" #include "esphome/core/optional.h" -#include "esphome/components/binary_sensor/binary_sensor.h" namespace esphome { namespace canbus { @@ -17,31 +16,7 @@ struct can_frame { uint8_t can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8))); }; - -class Canbus; - -class Canbus : public Component { - public: - /* special address description flags for the CAN_ID */ - static const uint32_t CAN_EFF_FLAG = 0x80000000UL; /* EFF/SFF is set in the MSB */ - static const uint32_t CAN_RTR_FLAG = 0x40000000UL; /* remote transmission request */ - static const uint32_t CAN_ERR_FLAG = 0x20000000UL; /* error message frame */ - - /* valid bits in CAN ID for frame formats */ - static const uint32_t CAN_SFF_MASK = 0x000007FFUL; /* standard frame format (SFF) */ - static const uint32_t CAN_EFF_MASK = 0x1FFFFFFFUL; /* extended frame format (EFF) */ - static const uint32_t CAN_ERR_MASK = 0x1FFFFFFFUL; /* omit EFF, RTR, ERR flags */ - - /* - * Controller Area Network Identifier structure - * - * bit 0-28 : CAN identifier (11/29 bit) - * bit 29 : error message frame flag (0 = data frame, 1 = error message) - * bit 30 : remote transmission request flag (1 = rtr frame) - * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) - */ - - enum CANSPEED : uint8_t { + enum CAN_SPEED : uint8_t { CAN_5KBPS, CAN_10KBPS, CAN_20KBPS, @@ -59,6 +34,7 @@ class Canbus : public Component { CAN_500KBPS, CAN_1000KBPS }; + enum ERROR : uint8_t { ERROR_OK = 0, ERROR_FAIL = 1, @@ -67,6 +43,18 @@ class Canbus : public Component { ERROR_FAILTX = 4, ERROR_NOMSG = 5 }; + /* special address description flags for the CAN_ID */ + static const uint32_t CAN_EFF_FLAG = 0x80000000UL; /* EFF/SFF is set in the MSB */ + static const uint32_t CAN_RTR_FLAG = 0x40000000UL; /* remote transmission request */ + static const uint32_t CAN_ERR_FLAG = 0x20000000UL; /* error message frame */ + + /* valid bits in CAN ID for frame formats */ + static const uint32_t CAN_SFF_MASK = 0x000007FFUL; /* standard frame format (SFF) */ + static const uint32_t CAN_EFF_MASK = 0x1FFFFFFFUL; /* extended frame format (EFF) */ + static const uint32_t CAN_ERR_MASK = 0x1FFFFFFFUL; /* omit EFF, RTR, ERR flags */ + +class Canbus : public Component { + public: Canbus(){}; Canbus(const std::string &name){}; @@ -77,20 +65,19 @@ class Canbus : public Component { void send_data(uint32_t can_id, const std::vector data); void set_sender_id(int sender_id) { this->sender_id_ = sender_id; } - void set_bitrate(uint8_t bit_rate) { this->bit_rate_ = bit_rate; } + void set_bitrate(CAN_SPEED bit_rate) { this->bit_rate_ = bit_rate; } protected: uint32_t sender_id_{0}; - uint8_t bit_rate_{CAN_125KBPS}; + CAN_SPEED bit_rate_{CAN_125KBPS}; - void dump_frame_(const struct can_frame *data_frame); virtual bool setup_internal_(); virtual ERROR send_message_(const struct can_frame *frame); }; -class CanbusTrigger : public Trigger, public Component { +class CanbusTrigger : public Trigger, public Component { public: - explicit CanbusTrigger(const uint32_t &can_id); + explicit CanbusTrigger(const std::uint32_t &can_id); void setup() override; void dump_config() override; diff --git a/esphome/components/mcp2515/canbus.py b/esphome/components/mcp2515/canbus.py index 584265e99c..495d8b798e 100644 --- a/esphome/components/mcp2515/canbus.py +++ b/esphome/components/mcp2515/canbus.py @@ -1,20 +1,26 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import spi, canbus -from esphome.automation import LambdaAction -from esphome import automation -from esphome.const import CONF_ID, CONF_TRIGGER_ID -from esphome.components.canbus import CanbusComponent,CONF_ON_RECEIVE, CONF_CAN_ID +from esphome.const import CONF_ID +from esphome.components.canbus import CanbusComponent, CAN_SPEEDS, CONF_BIT_RATE -print("mcp2515.canbus.py") AUTO_LOAD = ['canbus'] DEPENDENCIES = ['spi'] +CONF_MCP_CLOCK = 'clock' + mcp2515_ns = cg.esphome_ns.namespace('mcp2515') mcp2515 = mcp2515_ns.class_('MCP2515', CanbusComponent, spi.SPIDevice) +CanClock = mcp2515_ns.enum('CAN_CLOCK') +CAN_CLOCK = { + '20MHZ': CanClock.MCP_20MHZ, + '16MHZ': CanClock.MCP_16MHZ, + '8MHZ':CanClock.MCP_8MHZ, +} CONFIG_SCHEMA = canbus.CONFIG_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(mcp2515), + cv.Optional(CONF_MCP_CLOCK, default='8MHZ'): cv.enum(CAN_CLOCK, upper=True), }).extend(spi.SPI_DEVICE_SCHEMA) @@ -22,8 +28,9 @@ def to_code(config): rhs = mcp2515.new() var = cg.Pvariable(config[CONF_ID], rhs) yield canbus.register_canbus(var, config) + # canclock = CAN_CLOCK[config[CONF_MCP_CLOCK]] + # bitrate = CAN_SPEEDS[config[CONF_BIT_RATE]] + # cg.add(var.set_bitrate(bitrate,canclock)) + yield spi.register_spi_device(var, config) - for conf in config.get(CONF_ON_RECEIVE, []): - trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_CAN_ID]) - yield cg.register_component(trig, conf) - yield automation.build_automation(trig, [(cg.std_string, 'x')], conf) + diff --git a/esphome/components/mcp2515/mcp2515.cpp b/esphome/components/mcp2515/mcp2515.cpp index 2188d6a03f..58b87448e0 100644 --- a/esphome/components/mcp2515/mcp2515.cpp +++ b/esphome/components/mcp2515/mcp2515.cpp @@ -17,7 +17,7 @@ bool MCP2515::setup_internal_() { ESP_LOGD(TAG, "setup_internal_()"); this->spi_setup(); - if (this->reset_() == ERROR_FAIL) + if (this->reset_() == canbus::ERROR_FAIL) return false; this->set_bitrate_(this->bit_rate_); this->set_normal_mode_(); @@ -37,7 +37,7 @@ bool MCP2515::setup_internal_() { return true; } -MCP2515::ERROR MCP2515::reset_(void) { +canbus::ERROR MCP2515::reset_(void) { this->enable(); this->transfer_byte(INSTRUCTION_RESET); this->disable(); @@ -64,21 +64,21 @@ MCP2515::ERROR MCP2515::reset_(void) { // clear filters and masks /*RXF filters[] = {RXF0, RXF1, RXF2, RXF3, RXF4, RXF5}; for (int i=0; i<6; i++) { - ERROR result = set_filter_(filters[i], true, 0); - if (result != ERROR_OK) { + canbus::ERROR result = set_filter_(filters[i], true, 0); + if (result != canbus::ERROR_OK) { return result; } } MASK masks[] = {MASK0, MASK1}; for (int i=0; i<2; i++) { - ERROR result = set_filter_mask_(masks[i], true, 0); - if (result != ERROR_OK) { + canbus::ERROR result = set_filter_mask_(masks[i], true, 0); + if (result != canbus::ERROR_OK) { return result; } }*/ - return ERROR_OK; + return canbus::ERROR_OK; } uint8_t MCP2515::read_register_(const REGISTER reg) { @@ -140,17 +140,17 @@ uint8_t MCP2515::get_status_(void) { return i; } -MCP2515::ERROR MCP2515::set_config_mode_() { return set_mode_(CANCTRL_REQOP_CONFIG); } +canbus::ERROR MCP2515::set_config_mode_() { return set_mode_(CANCTRL_REQOP_CONFIG); } -MCP2515::ERROR MCP2515::set_listen_only_() { return set_mode_(CANCTRL_REQOP_LISTENONLY); } +canbus::ERROR MCP2515::set_listen_only_() { return set_mode_(CANCTRL_REQOP_LISTENONLY); } -MCP2515::ERROR MCP2515::set_sleep_mode_() { return set_mode_(CANCTRL_REQOP_SLEEP); } +canbus::ERROR MCP2515::set_sleep_mode_() { return set_mode_(CANCTRL_REQOP_SLEEP); } -MCP2515::ERROR MCP2515::set_loop_back_mode_() { return set_mode_(CANCTRL_REQOP_LOOPBACK); } +canbus::ERROR MCP2515::set_loop_back_mode_() { return set_mode_(CANCTRL_REQOP_LOOPBACK); } -MCP2515::ERROR MCP2515::set_normal_mode_() { return set_mode_(CANCTRL_REQOP_NORMAL); } +canbus::ERROR MCP2515::set_normal_mode_() { return set_mode_(CANCTRL_REQOP_NORMAL); } -MCP2515::ERROR MCP2515::set_mode_(const CANCTRL_REQOP_MODE mode) { +canbus::ERROR MCP2515::set_mode_(const CANCTRL_REQOP_MODE mode) { modify_register_(MCP_CANCTRL, CANCTRL_REQOP, mode); unsigned long endTime = millis() + 10; @@ -163,11 +163,11 @@ MCP2515::ERROR MCP2515::set_mode_(const CANCTRL_REQOP_MODE mode) { break; } } - return modeMatch ? ERROR_OK : ERROR_FAIL; + return modeMatch ? canbus::ERROR_OK : canbus::ERROR_FAIL; } -MCP2515::ERROR MCP2515::set_clk_out_(const CAN_CLKOUT divisor) { - ERROR res; +canbus::ERROR MCP2515::set_clk_out_(const CAN_CLKOUT divisor) { + canbus::ERROR res; uint8_t cfg3; if (divisor == CLKOUT_DISABLE) { @@ -176,7 +176,7 @@ MCP2515::ERROR MCP2515::set_clk_out_(const CAN_CLKOUT divisor) { /* Turn on CLKOUT for SOF */ modify_register_(MCP_CNF3, CNF3_SOF, CNF3_SOF); - return ERROR_OK; + return canbus::ERROR_OK; } /* Set the prescaler (CLKPRE) */ @@ -187,7 +187,7 @@ MCP2515::ERROR MCP2515::set_clk_out_(const CAN_CLKOUT divisor) { /* Turn off CLKOUT for SOF */ modify_register_(MCP_CNF3, CNF3_SOF, 0x00); - return ERROR_OK; + return canbus::ERROR_OK; } void MCP2515::prepare_id_(uint8_t *buffer, const bool ext, const uint32_t id) { @@ -209,9 +209,9 @@ void MCP2515::prepare_id_(uint8_t *buffer, const bool ext, const uint32_t id) { } } -MCP2515::ERROR MCP2515::set_filter_mask_(const MASK mask, const bool ext, const uint32_t ulData) { - ERROR res = set_config_mode_(); - if (res != ERROR_OK) { +canbus::ERROR MCP2515::set_filter_mask_(const MASK mask, const bool ext, const uint32_t ulData) { + canbus::ERROR res = set_config_mode_(); + if (res != canbus::ERROR_OK) { return res; } @@ -227,17 +227,17 @@ MCP2515::ERROR MCP2515::set_filter_mask_(const MASK mask, const bool ext, const reg = MCP_RXM1SIDH; break; default: - return ERROR_FAIL; + return canbus::ERROR_FAIL; } set_registers_(reg, tbufdata, 4); - return ERROR_OK; + return canbus::ERROR_OK; } -MCP2515::ERROR MCP2515::set_filter_(const RXF num, const bool ext, const uint32_t ulData) { - ERROR res = set_config_mode_(); - if (res != ERROR_OK) { +canbus::ERROR MCP2515::set_filter_(const RXF num, const bool ext, const uint32_t ulData) { + canbus::ERROR res = set_config_mode_(); + if (res != canbus::ERROR_OK) { return res; } @@ -263,37 +263,37 @@ MCP2515::ERROR MCP2515::set_filter_(const RXF num, const bool ext, const uint32_ reg = MCP_RXF5SIDH; break; default: - return ERROR_FAIL; + return canbus::ERROR_FAIL; } uint8_t tbufdata[4]; prepare_id_(tbufdata, ext, ulData); set_registers_(reg, tbufdata, 4); - return ERROR_OK; + return canbus::ERROR_OK; } -MCP2515::ERROR MCP2515::send_message_(const TXBn txbn, const struct canbus::can_frame *frame) { +canbus::ERROR MCP2515::send_message_(const TXBn txbn, const struct canbus::can_frame *frame) { const struct TXBn_REGS *txbuf = &TXB[txbn]; uint8_t data[13]; - bool ext = (frame->can_id & CAN_EFF_FLAG); - bool rtr = (frame->can_id & CAN_RTR_FLAG); - uint32_t id = (frame->can_id & (ext ? CAN_EFF_MASK : CAN_SFF_MASK)); + bool ext = (frame->can_id & canbus::CAN_EFF_FLAG); + bool rtr = (frame->can_id & canbus::CAN_RTR_FLAG); + uint32_t id = (frame->can_id & (ext ? canbus::CAN_EFF_MASK : canbus::CAN_SFF_MASK)); prepare_id_(data, ext, id); data[MCP_DLC] = rtr ? (frame->can_dlc | RTR_MASK) : frame->can_dlc; memcpy(&data[MCP_DATA], frame->data, frame->can_dlc); set_registers_(txbuf->SIDH, data, 5 + frame->can_dlc); modify_register_(txbuf->CTRL, TXB_TXREQ, TXB_TXREQ); - return ERROR_OK; + return canbus::ERROR_OK; } -MCP2515::ERROR MCP2515::send_message_(const struct canbus::can_frame *frame) { +canbus::ERROR MCP2515::send_message_(const struct canbus::can_frame *frame) { //ESP_LOGD(TAG, "send_message_: frame.id = %d", frame->can_id); if (frame->can_dlc > canbus::CAN_MAX_DLEN) { - return ERROR_FAILTX; + return canbus::ERROR_FAILTX; } //ESP_LOGD(TAG, "send_message_: size = %d is OK", frame->can_dlc); TXBn txBuffers[N_TXBUFFERS] = {TXB0, TXB1, TXB2}; @@ -307,10 +307,10 @@ MCP2515::ERROR MCP2515::send_message_(const struct canbus::can_frame *frame) { } } - return ERROR_FAILTX; + return canbus::ERROR_FAILTX; } -MCP2515::ERROR MCP2515::read_message_(const RXBn rxbn, struct canbus::can_frame *frame) { +canbus::ERROR MCP2515::read_message_(const RXBn rxbn, struct canbus::can_frame *frame) { const struct RXBn_REGS *rxb = &RXB[rxbn]; uint8_t tbufdata[5]; @@ -323,17 +323,17 @@ MCP2515::ERROR MCP2515::read_message_(const RXBn rxbn, struct canbus::can_frame id = (id << 2) + (tbufdata[MCP_SIDL] & 0x03); id = (id << 8) + tbufdata[MCP_EID8]; id = (id << 8) + tbufdata[MCP_EID0]; - id |= CAN_EFF_FLAG; + id |= canbus::CAN_EFF_FLAG; } uint8_t dlc = (tbufdata[MCP_DLC] & DLC_MASK); if (dlc > canbus::CAN_MAX_DLEN) { - return ERROR_FAIL; + return canbus::ERROR_FAIL; } uint8_t ctrl = read_register_(rxb->CTRL); if (ctrl & RXBnCTRL_RTR) { - id |= CAN_RTR_FLAG; + id |= canbus::CAN_RTR_FLAG; } frame->can_id = id; @@ -343,11 +343,11 @@ MCP2515::ERROR MCP2515::read_message_(const RXBn rxbn, struct canbus::can_frame modify_register_(MCP_CANINTF, rxb->CANINTF_RXnIF, 0); - return ERROR_OK; + return canbus::ERROR_OK; } -MCP2515::ERROR MCP2515::read_message_(struct canbus::can_frame *frame) { - ERROR rc; +canbus::ERROR MCP2515::read_message_(struct canbus::can_frame *frame) { + canbus::ERROR rc; uint8_t stat = get_status_(); if (stat & STAT_RX0IF) { @@ -355,7 +355,7 @@ MCP2515::ERROR MCP2515::read_message_(struct canbus::can_frame *frame) { } else if (stat & STAT_RX1IF) { rc = read_message_(RXB1, frame); } else { - rc = ERROR_NOMSG; + rc = canbus::ERROR_NOMSG; } return rc; @@ -415,11 +415,11 @@ void MCP2515::clearERRIF() { modify_register_(MCP_CANINTF, CANINTF_ERRIF, 0); } -MCP2515::ERROR MCP2515::set_bitrate_(uint8_t can_speed) { return this->set_bitrate_(can_speed, MCP_16MHZ); } +canbus::ERROR MCP2515::set_bitrate_(canbus::CAN_SPEED can_speed) { return this->set_bitrate_(can_speed, MCP_16MHZ); } -MCP2515::ERROR MCP2515::set_bitrate_(uint8_t can_speed, CAN_CLOCK can_clock) { - ERROR error = set_config_mode_(); - if (error != ERROR_OK) { +canbus::ERROR MCP2515::set_bitrate_(canbus::CAN_SPEED can_speed, CAN_CLOCK can_clock) { + canbus::ERROR error = set_config_mode_(); + if (error != canbus::ERROR_OK) { return error; } @@ -428,72 +428,72 @@ MCP2515::ERROR MCP2515::set_bitrate_(uint8_t can_speed, CAN_CLOCK can_clock) { switch (can_clock) { case (MCP_8MHZ): switch (can_speed) { - case (CAN_5KBPS): // 5KBPS + case (canbus::CAN_5KBPS): // 5KBPS cfg1 = MCP_8MHz_5kBPS_CFG1; cfg2 = MCP_8MHz_5kBPS_CFG2; cfg3 = MCP_8MHz_5kBPS_CFG3; break; - case (CAN_10KBPS): // 10KBPS + case (canbus::CAN_10KBPS): // 10KBPS cfg1 = MCP_8MHz_10kBPS_CFG1; cfg2 = MCP_8MHz_10kBPS_CFG2; cfg3 = MCP_8MHz_10kBPS_CFG3; break; - case (CAN_20KBPS): // 20KBPS + case (canbus::CAN_20KBPS): // 20KBPS cfg1 = MCP_8MHz_20kBPS_CFG1; cfg2 = MCP_8MHz_20kBPS_CFG2; cfg3 = MCP_8MHz_20kBPS_CFG3; break; - case (CAN_31K25BPS): // 31.25KBPS + case (canbus::CAN_31K25BPS): // 31.25KBPS cfg1 = MCP_8MHz_31k25BPS_CFG1; cfg2 = MCP_8MHz_31k25BPS_CFG2; cfg3 = MCP_8MHz_31k25BPS_CFG3; break; - case (CAN_33KBPS): // 33.333KBPS + case (canbus::CAN_33KBPS): // 33.333KBPS cfg1 = MCP_8MHz_33k3BPS_CFG1; cfg2 = MCP_8MHz_33k3BPS_CFG2; cfg3 = MCP_8MHz_33k3BPS_CFG3; break; - case (CAN_40KBPS): // 40Kbps + case (canbus::CAN_40KBPS): // 40Kbps cfg1 = MCP_8MHz_40kBPS_CFG1; cfg2 = MCP_8MHz_40kBPS_CFG2; cfg3 = MCP_8MHz_40kBPS_CFG3; break; - case (CAN_50KBPS): // 50Kbps + case (canbus::CAN_50KBPS): // 50Kbps cfg1 = MCP_8MHz_50kBPS_CFG1; cfg2 = MCP_8MHz_50kBPS_CFG2; cfg3 = MCP_8MHz_50kBPS_CFG3; break; - case (CAN_80KBPS): // 80Kbps + case (canbus::CAN_80KBPS): // 80Kbps cfg1 = MCP_8MHz_80kBPS_CFG1; cfg2 = MCP_8MHz_80kBPS_CFG2; cfg3 = MCP_8MHz_80kBPS_CFG3; break; - case (CAN_100KBPS): // 100Kbps + case (canbus::CAN_100KBPS): // 100Kbps cfg1 = MCP_8MHz_100kBPS_CFG1; cfg2 = MCP_8MHz_100kBPS_CFG2; cfg3 = MCP_8MHz_100kBPS_CFG3; break; - case (CAN_125KBPS): // 125Kbps + case (canbus::CAN_125KBPS): // 125Kbps cfg1 = MCP_8MHz_125kBPS_CFG1; cfg2 = MCP_8MHz_125kBPS_CFG2; cfg3 = MCP_8MHz_125kBPS_CFG3; break; - case (CAN_200KBPS): // 200Kbps + case (canbus::CAN_200KBPS): // 200Kbps cfg1 = MCP_8MHz_200kBPS_CFG1; cfg2 = MCP_8MHz_200kBPS_CFG2; cfg3 = MCP_8MHz_200kBPS_CFG3; break; - case (CAN_250KBPS): // 250Kbps + case (canbus::CAN_250KBPS): // 250Kbps cfg1 = MCP_8MHz_250kBPS_CFG1; cfg2 = MCP_8MHz_250kBPS_CFG2; cfg3 = MCP_8MHz_250kBPS_CFG3; break; - case (CAN_500KBPS): // 500Kbps + case (canbus::CAN_500KBPS): // 500Kbps cfg1 = MCP_8MHz_500kBPS_CFG1; cfg2 = MCP_8MHz_500kBPS_CFG2; cfg3 = MCP_8MHz_500kBPS_CFG3; break; - case (CAN_1000KBPS): // 1Mbps + case (canbus::CAN_1000KBPS): // 1Mbps cfg1 = MCP_8MHz_1000kBPS_CFG1; cfg2 = MCP_8MHz_1000kBPS_CFG2; cfg3 = MCP_8MHz_1000kBPS_CFG3; @@ -506,71 +506,71 @@ MCP2515::ERROR MCP2515::set_bitrate_(uint8_t can_speed, CAN_CLOCK can_clock) { case (MCP_16MHZ): switch (can_speed) { - case (CAN_5KBPS): // 5Kbps + case (canbus::CAN_5KBPS): // 5Kbps cfg1 = MCP_16MHz_5kBPS_CFG1; cfg2 = MCP_16MHz_5kBPS_CFG2; cfg3 = MCP_16MHz_5kBPS_CFG3; break; - case (CAN_10KBPS): // 10Kbps + case (canbus::CAN_10KBPS): // 10Kbps cfg1 = MCP_16MHz_10kBPS_CFG1; cfg2 = MCP_16MHz_10kBPS_CFG2; cfg3 = MCP_16MHz_10kBPS_CFG3; break; - case (CAN_20KBPS): // 20Kbps + case (canbus::CAN_20KBPS): // 20Kbps cfg1 = MCP_16MHz_20kBPS_CFG1; cfg2 = MCP_16MHz_20kBPS_CFG2; cfg3 = MCP_16MHz_20kBPS_CFG3; break; - case (CAN_33KBPS): // 33.333Kbps + case (canbus::CAN_33KBPS): // 33.333Kbps cfg1 = MCP_16MHz_33k3BPS_CFG1; cfg2 = MCP_16MHz_33k3BPS_CFG2; cfg3 = MCP_16MHz_33k3BPS_CFG3; break; - case (CAN_40KBPS): // 40Kbps + case (canbus::CAN_40KBPS): // 40Kbps cfg1 = MCP_16MHz_40kBPS_CFG1; cfg2 = MCP_16MHz_40kBPS_CFG2; cfg3 = MCP_16MHz_40kBPS_CFG3; break; - case (CAN_50KBPS): // 50Kbps + case (canbus::CAN_50KBPS): // 50Kbps cfg2 = MCP_16MHz_50kBPS_CFG2; cfg3 = MCP_16MHz_50kBPS_CFG3; break; - case (CAN_80KBPS): // 80Kbps + case (canbus::CAN_80KBPS): // 80Kbps cfg1 = MCP_16MHz_80kBPS_CFG1; cfg2 = MCP_16MHz_80kBPS_CFG2; cfg3 = MCP_16MHz_80kBPS_CFG3; break; - case (CAN_83K3BPS): // 83.333Kbps + case (canbus::CAN_83K3BPS): // 83.333Kbps cfg1 = MCP_16MHz_83k3BPS_CFG1; cfg2 = MCP_16MHz_83k3BPS_CFG2; cfg3 = MCP_16MHz_83k3BPS_CFG3; break; - case (CAN_100KBPS): // 100Kbps + case (canbus::CAN_100KBPS): // 100Kbps cfg1 = MCP_16MHz_100kBPS_CFG1; cfg2 = MCP_16MHz_100kBPS_CFG2; cfg3 = MCP_16MHz_100kBPS_CFG3; break; - case (CAN_125KBPS): // 125Kbps + case (canbus::CAN_125KBPS): // 125Kbps cfg1 = MCP_16MHz_125kBPS_CFG1; cfg2 = MCP_16MHz_125kBPS_CFG2; cfg3 = MCP_16MHz_125kBPS_CFG3; break; - case (CAN_200KBPS): // 200Kbps + case (canbus::CAN_200KBPS): // 200Kbps cfg1 = MCP_16MHz_200kBPS_CFG1; cfg2 = MCP_16MHz_200kBPS_CFG2; cfg3 = MCP_16MHz_200kBPS_CFG3; break; - case (CAN_250KBPS): // 250Kbps + case (canbus::CAN_250KBPS): // 250Kbps cfg1 = MCP_16MHz_250kBPS_CFG1; cfg2 = MCP_16MHz_250kBPS_CFG2; cfg3 = MCP_16MHz_250kBPS_CFG3; break; - case (CAN_500KBPS): // 500Kbps + case (canbus::CAN_500KBPS): // 500Kbps cfg1 = MCP_16MHz_500kBPS_CFG1; cfg2 = MCP_16MHz_500kBPS_CFG2; cfg3 = MCP_16MHz_500kBPS_CFG3; break; - case (CAN_1000KBPS): // 1Mbps + case (canbus::CAN_1000KBPS): // 1Mbps cfg1 = MCP_16MHz_1000kBPS_CFG1; cfg2 = MCP_16MHz_1000kBPS_CFG2; cfg3 = MCP_16MHz_1000kBPS_CFG3; @@ -583,57 +583,57 @@ MCP2515::ERROR MCP2515::set_bitrate_(uint8_t can_speed, CAN_CLOCK can_clock) { case (MCP_20MHZ): switch (can_speed) { - case (CAN_33KBPS): // 33.333Kbps + case (canbus::CAN_33KBPS): // 33.333Kbps cfg1 = MCP_20MHz_33k3BPS_CFG1; cfg2 = MCP_20MHz_33k3BPS_CFG2; cfg3 = MCP_20MHz_33k3BPS_CFG3; break; - case (CAN_40KBPS): // 40Kbps + case (canbus::CAN_40KBPS): // 40Kbps cfg1 = MCP_20MHz_40kBPS_CFG1; cfg2 = MCP_20MHz_40kBPS_CFG2; cfg3 = MCP_20MHz_40kBPS_CFG3; break; - case (CAN_50KBPS): // 50Kbps + case (canbus::CAN_50KBPS): // 50Kbps cfg1 = MCP_20MHz_50kBPS_CFG1; cfg2 = MCP_20MHz_50kBPS_CFG2; cfg3 = MCP_20MHz_50kBPS_CFG3; break; - case (CAN_80KBPS): // 80Kbps + case (canbus::CAN_80KBPS): // 80Kbps cfg1 = MCP_20MHz_80kBPS_CFG1; cfg2 = MCP_20MHz_80kBPS_CFG2; cfg3 = MCP_20MHz_80kBPS_CFG3; break; - case (CAN_83K3BPS): // 83.333Kbps + case (canbus::CAN_83K3BPS): // 83.333Kbps cfg1 = MCP_20MHz_83k3BPS_CFG1; cfg2 = MCP_20MHz_83k3BPS_CFG2; cfg3 = MCP_20MHz_83k3BPS_CFG3; break; - case (CAN_100KBPS): // 100Kbps + case (canbus::CAN_100KBPS): // 100Kbps cfg1 = MCP_20MHz_100kBPS_CFG1; cfg2 = MCP_20MHz_100kBPS_CFG2; cfg3 = MCP_20MHz_100kBPS_CFG3; break; - case (CAN_125KBPS): // 125Kbps + case (canbus::CAN_125KBPS): // 125Kbps cfg1 = MCP_20MHz_125kBPS_CFG1; cfg2 = MCP_20MHz_125kBPS_CFG2; cfg3 = MCP_20MHz_125kBPS_CFG3; break; - case (CAN_200KBPS): // 200Kbps + case (canbus::CAN_200KBPS): // 200Kbps cfg1 = MCP_20MHz_200kBPS_CFG1; cfg2 = MCP_20MHz_200kBPS_CFG2; cfg3 = MCP_20MHz_200kBPS_CFG3; break; - case (CAN_250KBPS): // 250Kbps + case (canbus::CAN_250KBPS): // 250Kbps cfg1 = MCP_20MHz_250kBPS_CFG1; cfg2 = MCP_20MHz_250kBPS_CFG2; cfg3 = MCP_20MHz_250kBPS_CFG3; break; - case (CAN_500KBPS): // 500Kbps + case (canbus::CAN_500KBPS): // 500Kbps cfg1 = MCP_20MHz_500kBPS_CFG1; cfg2 = MCP_20MHz_500kBPS_CFG2; cfg3 = MCP_20MHz_500kBPS_CFG3; break; - case (CAN_1000KBPS): // 1Mbps + case (canbus::CAN_1000KBPS): // 1Mbps cfg1 = MCP_20MHz_1000kBPS_CFG1; cfg2 = MCP_20MHz_1000kBPS_CFG2; cfg3 = MCP_20MHz_1000kBPS_CFG3; @@ -653,9 +653,9 @@ MCP2515::ERROR MCP2515::set_bitrate_(uint8_t can_speed, CAN_CLOCK can_clock) { set_register_(MCP_CNF1, cfg1); set_register_(MCP_CNF2, cfg2); set_register_(MCP_CNF3, cfg3); - return ERROR_OK; + return canbus::ERROR_OK; } else { - return ERROR_FAIL; + return canbus::ERROR_FAIL; } } } // namespace mcp2515 diff --git a/esphome/components/mcp2515/mcp2515.h b/esphome/components/mcp2515/mcp2515.h index 89ab3aa91a..a040bc5daf 100644 --- a/esphome/components/mcp2515/mcp2515.h +++ b/esphome/components/mcp2515/mcp2515.h @@ -51,6 +51,7 @@ enum /*class*/ STAT : uint8_t { STAT_RX0IF = (1 << 0), STAT_RX1IF = (1 << 1) }; static const uint8_t STAT_RXIF_MASK = STAT_RX0IF | STAT_RX1IF; static const uint8_t EFLG_ERRORMASK = EFLG_RX1OVR | EFLG_RX0OVR | EFLG_TXBO | EFLG_TXEP | EFLG_RXEP; + class MCP2515 : public canbus::Canbus, public spi::SPIDevice { @@ -72,7 +73,7 @@ class MCP2515 : public canbus::Canbus, protected: bool setup_internal_() override; - ERROR set_mode_(const CANCTRL_REQOP_MODE mode); + canbus::ERROR set_mode_(const CANCTRL_REQOP_MODE mode); uint8_t read_register_(const REGISTER reg); void read_registers_(const REGISTER reg, uint8_t values[], const uint8_t n); @@ -81,21 +82,21 @@ class MCP2515 : public canbus::Canbus, void modify_register_(const REGISTER reg, const uint8_t mask, const uint8_t data); void prepare_id_(uint8_t *buffer, const bool ext, const uint32_t id); - ERROR reset_(void); - ERROR set_config_mode_(); - ERROR set_listen_only_(); - ERROR set_sleep_mode_(); - ERROR set_loop_back_mode_(); - ERROR set_normal_mode_(); - ERROR set_clk_out_(const CAN_CLKOUT divisor); - ERROR set_bitrate_(uint8_t can_speed); - ERROR set_bitrate_(uint8_t can_speed, const CAN_CLOCK can_clock); - ERROR set_filter_mask_(const MASK num, const bool ext, const uint32_t ulData); - ERROR set_filter_(const RXF num, const bool ext, const uint32_t ulData); - ERROR send_message_(const TXBn txbn, const struct canbus::can_frame *frame); - ERROR send_message_(const struct canbus::can_frame *frame); - ERROR read_message_(const RXBn rxbn, struct canbus::can_frame *frame); - ERROR read_message_(struct canbus::can_frame *frame); + canbus::ERROR reset_(void); + canbus::ERROR set_config_mode_(); + canbus::ERROR set_listen_only_(); + canbus::ERROR set_sleep_mode_(); + canbus::ERROR set_loop_back_mode_(); + canbus::ERROR set_normal_mode_(); + canbus::ERROR set_clk_out_(const CAN_CLKOUT divisor); + canbus::ERROR set_bitrate_(canbus::CAN_SPEED can_speed); + canbus::ERROR set_bitrate_(canbus::CAN_SPEED can_speed, const CAN_CLOCK can_clock); + canbus::ERROR set_filter_mask_(const MASK num, const bool ext, const uint32_t ulData); + canbus::ERROR set_filter_(const RXF num, const bool ext, const uint32_t ulData); + canbus::ERROR send_message_(const TXBn txbn, const struct canbus::can_frame *frame); + canbus::ERROR send_message_(const struct canbus::can_frame *frame); + canbus::ERROR read_message_(const RXBn rxbn, struct canbus::can_frame *frame); + canbus::ERROR read_message_(struct canbus::can_frame *frame); bool check_receive_(void); bool check_error_(void); uint8_t get_error_flags_(void);