mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Extend ESP32 CAN bit rates /bus speed support (#5280)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: mkaiser <mkaiser@techfak.uni-bielefeld.de>
This commit is contained in:
parent
a9630ac847
commit
d382ca2401
4 changed files with 86 additions and 3 deletions
|
@ -45,9 +45,13 @@ CanbusTrigger = canbus_ns.class_(
|
|||
CanSpeed = canbus_ns.enum("CAN_SPEED")
|
||||
|
||||
CAN_SPEEDS = {
|
||||
"1KBPS": CanSpeed.CAN_1KBPS,
|
||||
"5KBPS": CanSpeed.CAN_5KBPS,
|
||||
"10KBPS": CanSpeed.CAN_10KBPS,
|
||||
"12K5BPS": CanSpeed.CAN_12K5BPS,
|
||||
"16KBPS": CanSpeed.CAN_16KBPS,
|
||||
"20KBPS": CanSpeed.CAN_20KBPS,
|
||||
"25KBPS": CanSpeed.CAN_25KBPS,
|
||||
"31K25BPS": CanSpeed.CAN_31K25BPS,
|
||||
"33KBPS": CanSpeed.CAN_33KBPS,
|
||||
"40KBPS": CanSpeed.CAN_40KBPS,
|
||||
|
@ -60,9 +64,9 @@ CAN_SPEEDS = {
|
|||
"200KBPS": CanSpeed.CAN_200KBPS,
|
||||
"250KBPS": CanSpeed.CAN_250KBPS,
|
||||
"500KBPS": CanSpeed.CAN_500KBPS,
|
||||
"800KBPS": CanSpeed.CAN_800KBPS,
|
||||
"1000KBPS": CanSpeed.CAN_1000KBPS,
|
||||
}
|
||||
|
||||
CANBUS_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(CanbusComponent),
|
||||
|
|
|
@ -19,9 +19,13 @@ enum Error : uint8_t {
|
|||
};
|
||||
|
||||
enum CanSpeed : uint8_t {
|
||||
CAN_1KBPS,
|
||||
CAN_5KBPS,
|
||||
CAN_10KBPS,
|
||||
CAN_12K5BPS,
|
||||
CAN_16KBPS,
|
||||
CAN_20KBPS,
|
||||
CAN_25KBPS,
|
||||
CAN_31K25BPS,
|
||||
CAN_33KBPS,
|
||||
CAN_40KBPS,
|
||||
|
@ -34,6 +38,7 @@ enum CanSpeed : uint8_t {
|
|||
CAN_200KBPS,
|
||||
CAN_250KBPS,
|
||||
CAN_500KBPS,
|
||||
CAN_800KBPS,
|
||||
CAN_1000KBPS
|
||||
};
|
||||
|
||||
|
|
|
@ -5,6 +5,15 @@ from esphome.components import canbus
|
|||
from esphome.const import CONF_ID, CONF_RX_PIN, CONF_TX_PIN
|
||||
from esphome.components.canbus import CanbusComponent, CanSpeed, CONF_BIT_RATE
|
||||
|
||||
from esphome.components.esp32 import get_esp32_variant
|
||||
from esphome.components.esp32.const import (
|
||||
VARIANT_ESP32,
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
VARIANT_ESP32C3,
|
||||
VARIANT_ESP32H2,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@Sympatron"]
|
||||
DEPENDENCIES = ["esp32"]
|
||||
|
||||
|
@ -12,19 +21,57 @@ esp32_can_ns = cg.esphome_ns.namespace("esp32_can")
|
|||
esp32_can = esp32_can_ns.class_("ESP32Can", CanbusComponent)
|
||||
|
||||
# Currently the driver only supports a subset of the bit rates defined in canbus
|
||||
CAN_SPEEDS = {
|
||||
# The supported bit rates differ between ESP32 variants.
|
||||
# See ESP-IDF Programming Guide --> API Reference --> Two-Wire Automotive Interface (TWAI)
|
||||
|
||||
CAN_SPEEDS_ESP32 = {
|
||||
"25KBPS": CanSpeed.CAN_25KBPS,
|
||||
"50KBPS": CanSpeed.CAN_50KBPS,
|
||||
"100KBPS": CanSpeed.CAN_100KBPS,
|
||||
"125KBPS": CanSpeed.CAN_125KBPS,
|
||||
"250KBPS": CanSpeed.CAN_250KBPS,
|
||||
"500KBPS": CanSpeed.CAN_500KBPS,
|
||||
"800KBPS": CanSpeed.CAN_800KBPS,
|
||||
"1000KBPS": CanSpeed.CAN_1000KBPS,
|
||||
}
|
||||
|
||||
CAN_SPEEDS_ESP32_S2 = {
|
||||
"1KBPS": CanSpeed.CAN_1KBPS,
|
||||
"5KBPS": CanSpeed.CAN_5KBPS,
|
||||
"10KBPS": CanSpeed.CAN_10KBPS,
|
||||
"12K5BPS": CanSpeed.CAN_12K5BPS,
|
||||
"16KBPS": CanSpeed.CAN_16KBPS,
|
||||
"20KBPS": CanSpeed.CAN_20KBPS,
|
||||
**CAN_SPEEDS_ESP32,
|
||||
}
|
||||
|
||||
CAN_SPEEDS_ESP32_S3 = {**CAN_SPEEDS_ESP32_S2}
|
||||
CAN_SPEEDS_ESP32_C3 = {**CAN_SPEEDS_ESP32_S2}
|
||||
CAN_SPEEDS_ESP32_H2 = {**CAN_SPEEDS_ESP32_S2}
|
||||
|
||||
CAN_SPEEDS = {
|
||||
VARIANT_ESP32: CAN_SPEEDS_ESP32,
|
||||
VARIANT_ESP32S2: CAN_SPEEDS_ESP32_S2,
|
||||
VARIANT_ESP32S3: CAN_SPEEDS_ESP32_S3,
|
||||
VARIANT_ESP32C3: CAN_SPEEDS_ESP32_C3,
|
||||
VARIANT_ESP32H2: CAN_SPEEDS_ESP32_H2,
|
||||
}
|
||||
|
||||
|
||||
def validate_bit_rate(value):
|
||||
variant = get_esp32_variant()
|
||||
if variant not in CAN_SPEEDS:
|
||||
raise cv.Invalid(f"{variant} is not supported by component {esp32_can_ns}")
|
||||
value = value.upper()
|
||||
if value not in CAN_SPEEDS[variant]:
|
||||
raise cv.Invalid(f"Bit rate {value} is not supported on {variant}")
|
||||
return cv.enum(CAN_SPEEDS[variant])(value)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = canbus.CANBUS_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(esp32_can),
|
||||
cv.Optional(CONF_BIT_RATE, default="125KBPS"): cv.enum(CAN_SPEEDS, upper=True),
|
||||
cv.Optional(CONF_BIT_RATE, default="125KBPS"): validate_bit_rate,
|
||||
cv.Required(CONF_RX_PIN): pins.internal_gpio_input_pin_number,
|
||||
cv.Required(CONF_TX_PIN): pins.internal_gpio_output_pin_number,
|
||||
}
|
||||
|
|
|
@ -16,6 +16,30 @@ static const char *const TAG = "esp32_can";
|
|||
|
||||
static bool get_bitrate(canbus::CanSpeed bitrate, twai_timing_config_t *t_config) {
|
||||
switch (bitrate) {
|
||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32C3) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32H6)
|
||||
case canbus::CAN_1KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1KBITS();
|
||||
return true;
|
||||
case canbus::CAN_5KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_5KBITS();
|
||||
return true;
|
||||
case canbus::CAN_10KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_10KBITS();
|
||||
return true;
|
||||
case canbus::CAN_12K5BPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_12_5KBITS();
|
||||
return true;
|
||||
case canbus::CAN_16KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_16KBITS();
|
||||
return true;
|
||||
case canbus::CAN_20KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_20KBITS();
|
||||
return true;
|
||||
#endif
|
||||
case canbus::CAN_25KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_25KBITS();
|
||||
return true;
|
||||
case canbus::CAN_50KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_50KBITS();
|
||||
return true;
|
||||
|
@ -31,6 +55,9 @@ static bool get_bitrate(canbus::CanSpeed bitrate, twai_timing_config_t *t_config
|
|||
case canbus::CAN_500KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_500KBITS();
|
||||
return true;
|
||||
case canbus::CAN_800KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_800KBITS();
|
||||
return true;
|
||||
case canbus::CAN_1000KBPS:
|
||||
*t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1MBITS();
|
||||
return true;
|
||||
|
|
Loading…
Reference in a new issue