mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
code cleanup and lint
This commit is contained in:
parent
f74babeeba
commit
4e80a901ae
@ -1,23 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/optional.h"
|
#include "esphome/core/optional.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace canbus {
|
namespace canbus {
|
||||||
|
|
||||||
class CanbusTrigger;
|
enum ERROR : uint8_t {
|
||||||
|
ERROR_OK = 0,
|
||||||
/* CAN payload length and DLC definitions according to ISO 11898-1 */
|
ERROR_FAIL = 1,
|
||||||
static const uint8_t CAN_MAX_DLC = 8;
|
ERROR_ALLTXBUSY = 2,
|
||||||
static const uint8_t CAN_MAX_DLEN = 8;
|
ERROR_FAILINIT = 3,
|
||||||
|
ERROR_FAILTX = 4,
|
||||||
struct can_frame {
|
ERROR_NOMSG = 5
|
||||||
uint32_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
|
|
||||||
uint8_t can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
|
|
||||||
uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CAN_SPEED : uint8_t {
|
enum CAN_SPEED : uint8_t {
|
||||||
CAN_5KBPS,
|
CAN_5KBPS,
|
||||||
CAN_10KBPS,
|
CAN_10KBPS,
|
||||||
@ -37,23 +35,32 @@ enum CAN_SPEED : uint8_t {
|
|||||||
CAN_1000KBPS
|
CAN_1000KBPS
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ERROR : uint8_t {
|
|
||||||
ERROR_OK = 0,
|
|
||||||
ERROR_FAIL = 1,
|
|
||||||
ERROR_ALLTXBUSY = 2,
|
|
||||||
ERROR_FAILINIT = 3,
|
|
||||||
ERROR_FAILTX = 4,
|
|
||||||
ERROR_NOMSG = 5
|
|
||||||
};
|
|
||||||
/* special address description flags for the CAN_ID */
|
/* 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_EFF_FLAG =
|
||||||
static const uint32_t CAN_RTR_FLAG = 0x40000000UL; /* remote transmission request */
|
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 */
|
static const uint32_t CAN_ERR_FLAG = 0x20000000UL; /* error message frame */
|
||||||
|
|
||||||
/* valid bits in CAN ID for frame formats */
|
/* 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_SFF_MASK =
|
||||||
static const uint32_t CAN_EFF_MASK = 0x1FFFFFFFUL; /* extended frame format (EFF) */
|
0x000007FFUL; /* standard frame format (SFF) */
|
||||||
static const uint32_t CAN_ERR_MASK = 0x1FFFFFFFUL; /* omit EFF, RTR, ERR flags */
|
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 CanbusTrigger;
|
||||||
|
|
||||||
|
/* CAN payload length and DLC definitions according to ISO 11898-1 */
|
||||||
|
static const uint8_t CAN_MAX_DLC = 8;
|
||||||
|
static const uint8_t CAN_MAX_DLEN = 8;
|
||||||
|
|
||||||
|
struct can_frame {
|
||||||
|
uint32_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
|
||||||
|
uint8_t can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
|
||||||
|
uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
|
||||||
|
};
|
||||||
|
|
||||||
class Canbus : public Component {
|
class Canbus : public Component {
|
||||||
public:
|
public:
|
||||||
@ -113,11 +120,11 @@ protected:
|
|||||||
|
|
||||||
class CanbusTrigger : public Trigger<std::vector<uint8_t>>, public Component {
|
class CanbusTrigger : public Trigger<std::vector<uint8_t>>, public Component {
|
||||||
friend class Canbus;
|
friend class Canbus;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit CanbusTrigger(Canbus *parent, const std::uint32_t can_id):parent_(parent), can_id_(can_id){};
|
explicit CanbusTrigger(Canbus *parent, const std::uint32_t can_id)
|
||||||
void setup() override {
|
: parent_(parent), can_id_(can_id){};
|
||||||
this->parent_->add_trigger(this);
|
void setup() override { this->parent_->add_trigger(this); }
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Canbus *parent_;
|
Canbus *parent_;
|
||||||
|
@ -1 +0,0 @@
|
|||||||
print("esp32_can_module")
|
|
@ -1 +0,0 @@
|
|||||||
print("mcp2515.__init__.py")
|
|
@ -6,11 +6,13 @@ namespace mcp2515 {
|
|||||||
|
|
||||||
static const char *TAG = "mcp2515";
|
static const char *TAG = "mcp2515";
|
||||||
|
|
||||||
const struct MCP2515::TXBn_REGS MCP2515::TXB[N_TXBUFFERS] = {{MCP_TXB0CTRL, MCP_TXB0SIDH, MCP_TXB0DATA},
|
const struct MCP2515::TXBn_REGS MCP2515::TXB[N_TXBUFFERS] = {
|
||||||
|
{MCP_TXB0CTRL, MCP_TXB0SIDH, MCP_TXB0DATA},
|
||||||
{MCP_TXB1CTRL, MCP_TXB1SIDH, MCP_TXB1DATA},
|
{MCP_TXB1CTRL, MCP_TXB1SIDH, MCP_TXB1DATA},
|
||||||
{MCP_TXB2CTRL, MCP_TXB2SIDH, MCP_TXB2DATA}};
|
{MCP_TXB2CTRL, MCP_TXB2SIDH, MCP_TXB2DATA}};
|
||||||
|
|
||||||
const struct MCP2515::RXBn_REGS MCP2515::RXB[N_RXBUFFERS] = {{MCP_RXB0CTRL, MCP_RXB0SIDH, MCP_RXB0DATA, CANINTF_RX0IF},
|
const struct MCP2515::RXBn_REGS MCP2515::RXB[N_RXBUFFERS] = {
|
||||||
|
{MCP_RXB0CTRL, MCP_RXB0SIDH, MCP_RXB0DATA, CANINTF_RX0IF},
|
||||||
{MCP_RXB1CTRL, MCP_RXB1SIDH, MCP_RXB1DATA, CANINTF_RX1IF}};
|
{MCP_RXB1CTRL, MCP_RXB1SIDH, MCP_RXB1DATA, CANINTF_RX1IF}};
|
||||||
|
|
||||||
bool MCP2515::setup_internal_() {
|
bool MCP2515::setup_internal_() {
|
||||||
@ -44,9 +46,11 @@ canbus::ERROR MCP2515::reset_(void) {
|
|||||||
set_register_(MCP_RXB0CTRL, 0);
|
set_register_(MCP_RXB0CTRL, 0);
|
||||||
set_register_(MCP_RXB1CTRL, 0);
|
set_register_(MCP_RXB1CTRL, 0);
|
||||||
|
|
||||||
set_register_(MCP_CANINTE, CANINTF_RX0IF | CANINTF_RX1IF | CANINTF_ERRIF | CANINTF_MERRF);
|
set_register_(MCP_CANINTE,
|
||||||
|
CANINTF_RX0IF | CANINTF_RX1IF | CANINTF_ERRIF | CANINTF_MERRF);
|
||||||
|
|
||||||
modify_register_(MCP_RXB0CTRL, RXBnCTRL_RXM_MASK | RXB0CTRL_BUKT, RXBnCTRL_RXM_STDEXT | RXB0CTRL_BUKT);
|
modify_register_(MCP_RXB0CTRL, RXBnCTRL_RXM_MASK | RXB0CTRL_BUKT,
|
||||||
|
RXBnCTRL_RXM_STDEXT | RXB0CTRL_BUKT);
|
||||||
modify_register_(MCP_RXB1CTRL, RXBnCTRL_RXM_MASK, RXBnCTRL_RXM_STDEXT);
|
modify_register_(MCP_RXB1CTRL, RXBnCTRL_RXM_MASK, RXBnCTRL_RXM_STDEXT);
|
||||||
|
|
||||||
// clear filters and masks
|
// clear filters and masks
|
||||||
@ -79,7 +83,8 @@ uint8_t MCP2515::read_register_(const REGISTER reg) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCP2515::read_registers_(const REGISTER reg, uint8_t values[], const uint8_t n) {
|
void MCP2515::read_registers_(const REGISTER reg, uint8_t values[],
|
||||||
|
const uint8_t n) {
|
||||||
this->enable();
|
this->enable();
|
||||||
this->transfer_byte(INSTRUCTION_READ);
|
this->transfer_byte(INSTRUCTION_READ);
|
||||||
this->transfer_byte(reg);
|
this->transfer_byte(reg);
|
||||||
@ -99,7 +104,8 @@ void MCP2515::set_register_(const REGISTER reg, const uint8_t value) {
|
|||||||
this->disable();
|
this->disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCP2515::set_registers_(const REGISTER reg, uint8_t values[], const uint8_t n) {
|
void MCP2515::set_registers_(const REGISTER reg, uint8_t values[],
|
||||||
|
const uint8_t n) {
|
||||||
this->enable();
|
this->enable();
|
||||||
this->transfer_byte(INSTRUCTION_WRITE);
|
this->transfer_byte(INSTRUCTION_WRITE);
|
||||||
this->transfer_byte(reg);
|
this->transfer_byte(reg);
|
||||||
@ -110,7 +116,8 @@ void MCP2515::set_registers_(const REGISTER reg, uint8_t values[], const uint8_t
|
|||||||
this->disable();
|
this->disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCP2515::modify_register_(const REGISTER reg, const uint8_t mask, const uint8_t data) {
|
void MCP2515::modify_register_(const REGISTER reg, const uint8_t mask,
|
||||||
|
const uint8_t data) {
|
||||||
this->enable();
|
this->enable();
|
||||||
this->transfer_byte(INSTRUCTION_BITMOD);
|
this->transfer_byte(INSTRUCTION_BITMOD);
|
||||||
this->transfer_byte(reg);
|
this->transfer_byte(reg);
|
||||||
@ -187,7 +194,8 @@ void MCP2515::prepare_id_(uint8_t *buffer, const bool ext, const uint32_t id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
canbus::ERROR MCP2515::set_filter_mask_(const MASK mask, const bool ext, const uint32_t ulData) {
|
canbus::ERROR MCP2515::set_filter_mask_(const MASK mask, const bool ext,
|
||||||
|
const uint32_t ulData) {
|
||||||
canbus::ERROR res = set_mode_(CANCTRL_REQOP_CONFIG);
|
canbus::ERROR res = set_mode_(CANCTRL_REQOP_CONFIG);
|
||||||
if (res != canbus::ERROR_OK) {
|
if (res != canbus::ERROR_OK) {
|
||||||
return res;
|
return res;
|
||||||
@ -213,7 +221,8 @@ canbus::ERROR MCP2515::set_filter_mask_(const MASK mask, const bool ext, const u
|
|||||||
return canbus::ERROR_OK;
|
return canbus::ERROR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
canbus::ERROR MCP2515::set_filter_(const RXF num, const bool ext, const uint32_t ulData) {
|
canbus::ERROR MCP2515::set_filter_(const RXF num, const bool ext,
|
||||||
|
const uint32_t ulData) {
|
||||||
canbus::ERROR res = set_mode_(CANCTRL_REQOP_CONFIG);
|
canbus::ERROR res = set_mode_(CANCTRL_REQOP_CONFIG);
|
||||||
if (res != canbus::ERROR_OK) {
|
if (res != canbus::ERROR_OK) {
|
||||||
return res;
|
return res;
|
||||||
@ -251,14 +260,16 @@ canbus::ERROR MCP2515::set_filter_(const RXF num, const bool ext, const uint32_t
|
|||||||
return canbus::ERROR_OK;
|
return canbus::ERROR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
canbus::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];
|
const struct TXBn_REGS *txbuf = &TXB[txbn];
|
||||||
|
|
||||||
uint8_t data[13];
|
uint8_t data[13];
|
||||||
|
|
||||||
bool ext = (frame->can_id & canbus::CAN_EFF_FLAG);
|
bool ext = (frame->can_id & canbus::CAN_EFF_FLAG);
|
||||||
bool rtr = (frame->can_id & canbus::CAN_RTR_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));
|
uint32_t id =
|
||||||
|
(frame->can_id & (ext ? canbus::CAN_EFF_MASK : canbus::CAN_SFF_MASK));
|
||||||
prepare_id_(data, ext, id);
|
prepare_id_(data, ext, id);
|
||||||
data[MCP_DLC] = rtr ? (frame->can_dlc | RTR_MASK) : frame->can_dlc;
|
data[MCP_DLC] = rtr ? (frame->can_dlc | RTR_MASK) : frame->can_dlc;
|
||||||
memcpy(&data[MCP_DATA], frame->data, frame->can_dlc);
|
memcpy(&data[MCP_DATA], frame->data, frame->can_dlc);
|
||||||
@ -288,7 +299,8 @@ canbus::ERROR MCP2515::send_message_(const struct canbus::can_frame *frame) {
|
|||||||
return canbus::ERROR_FAILTX;
|
return canbus::ERROR_FAILTX;
|
||||||
}
|
}
|
||||||
|
|
||||||
canbus::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];
|
const struct RXBn_REGS *rxb = &RXB[rxbn];
|
||||||
|
|
||||||
uint8_t tbufdata[5];
|
uint8_t tbufdata[5];
|
||||||
@ -360,7 +372,9 @@ bool MCP2515::check_error_(void) {
|
|||||||
|
|
||||||
uint8_t MCP2515::get_error_flags_(void) { return read_register_(MCP_EFLG); }
|
uint8_t MCP2515::get_error_flags_(void) { return read_register_(MCP_EFLG); }
|
||||||
|
|
||||||
void MCP2515::clearRXnOVRFlags(void) { modify_register_(MCP_EFLG, EFLG_RX0OVR | EFLG_RX1OVR, 0); }
|
void MCP2515::clearRXnOVRFlags(void) {
|
||||||
|
modify_register_(MCP_EFLG, EFLG_RX0OVR | EFLG_RX1OVR, 0);
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t MCP2515::getInterrupts(void) { return read_register_(MCP_CANINTF); }
|
uint8_t MCP2515::getInterrupts(void) { return read_register_(MCP_CANINTF); }
|
||||||
|
|
||||||
@ -369,7 +383,8 @@ void MCP2515::clearInterrupts(void) { set_register_(MCP_CANINTF, 0); }
|
|||||||
uint8_t MCP2515::getInterruptMask(void) { return read_register_(MCP_CANINTE); }
|
uint8_t MCP2515::getInterruptMask(void) { return read_register_(MCP_CANINTE); }
|
||||||
|
|
||||||
void MCP2515::clearTXInterrupts(void) {
|
void MCP2515::clearTXInterrupts(void) {
|
||||||
modify_register_(MCP_CANINTF, (CANINTF_TX0IF | CANINTF_TX1IF | CANINTF_TX2IF), 0);
|
modify_register_(MCP_CANINTF, (CANINTF_TX0IF | CANINTF_TX1IF | CANINTF_TX2IF),
|
||||||
|
0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCP2515::clearRXnOVR(void) {
|
void MCP2515::clearRXnOVR(void) {
|
||||||
@ -393,9 +408,12 @@ void MCP2515::clearERRIF() {
|
|||||||
modify_register_(MCP_CANINTF, CANINTF_ERRIF, 0);
|
modify_register_(MCP_CANINTF, CANINTF_ERRIF, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
canbus::ERROR MCP2515::set_bitrate_(canbus::CAN_SPEED 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);
|
||||||
|
}
|
||||||
|
|
||||||
canbus::ERROR MCP2515::set_bitrate_(canbus::CAN_SPEED can_speed, CAN_CLOCK can_clock) {
|
canbus::ERROR MCP2515::set_bitrate_(canbus::CAN_SPEED can_speed,
|
||||||
|
CAN_CLOCK can_clock) {
|
||||||
canbus::ERROR error = set_mode_(CANCTRL_REQOP_CONFIG);
|
canbus::ERROR error = set_mode_(CANCTRL_REQOP_CONFIG);
|
||||||
if (error != canbus::ERROR_OK) {
|
if (error != canbus::ERROR_OK) {
|
||||||
return error;
|
return error;
|
||||||
|
@ -50,11 +50,13 @@ enum /*class*/ EFLG : uint8_t {
|
|||||||
enum /*class*/ STAT : uint8_t { STAT_RX0IF = (1 << 0), STAT_RX1IF = (1 << 1) };
|
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 STAT_RXIF_MASK = STAT_RX0IF | STAT_RX1IF;
|
||||||
static const uint8_t EFLG_ERRORMASK = EFLG_RX1OVR | EFLG_RX0OVR | EFLG_TXBO | EFLG_TXEP | EFLG_RXEP;
|
static const uint8_t EFLG_ERRORMASK =
|
||||||
|
EFLG_RX1OVR | EFLG_RX0OVR | EFLG_TXBO | EFLG_TXEP | EFLG_RXEP;
|
||||||
|
|
||||||
class MCP2515 : public canbus::Canbus,
|
class MCP2515
|
||||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
: public canbus::Canbus,
|
||||||
spi::DATA_RATE_8MHZ> {
|
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
|
||||||
|
spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_8MHZ> {
|
||||||
public:
|
public:
|
||||||
MCP2515(){};
|
MCP2515(){};
|
||||||
void set_mcp_clock(CAN_CLOCK clock) { this->mcp_clock_ = clock; };
|
void set_mcp_clock(CAN_CLOCK clock) { this->mcp_clock_ = clock; };
|
||||||
@ -82,16 +84,21 @@ class MCP2515 : public canbus::Canbus,
|
|||||||
void read_registers_(const REGISTER reg, uint8_t values[], const uint8_t n);
|
void read_registers_(const REGISTER reg, uint8_t values[], const uint8_t n);
|
||||||
void set_register_(const REGISTER reg, const uint8_t value);
|
void set_register_(const REGISTER reg, const uint8_t value);
|
||||||
void set_registers_(const REGISTER reg, uint8_t values[], const uint8_t n);
|
void set_registers_(const REGISTER reg, uint8_t values[], const uint8_t n);
|
||||||
void modify_register_(const REGISTER reg, const uint8_t mask, const uint8_t data);
|
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);
|
void prepare_id_(uint8_t *buffer, const bool ext, const uint32_t id);
|
||||||
canbus::ERROR reset_(void);
|
canbus::ERROR reset_(void);
|
||||||
canbus::ERROR set_clk_out_(const CAN_CLKOUT divisor);
|
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);
|
||||||
canbus::ERROR set_bitrate_(canbus::CAN_SPEED can_speed, const CAN_CLOCK can_clock);
|
canbus::ERROR set_bitrate_(canbus::CAN_SPEED can_speed,
|
||||||
canbus::ERROR set_filter_mask_(const MASK num, const bool ext, const uint32_t ulData);
|
const CAN_CLOCK can_clock);
|
||||||
canbus::ERROR set_filter_(const RXF num, const bool ext, const uint32_t ulData);
|
canbus::ERROR set_filter_mask_(const MASK num, const bool ext,
|
||||||
canbus::ERROR send_message_(const TXBn txbn, const struct canbus::can_frame *frame);
|
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 send_message_(const struct canbus::can_frame *frame);
|
||||||
canbus::ERROR read_message_(const RXBn rxbn, 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);
|
canbus::ERROR read_message_(struct canbus::can_frame *frame);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user