Skip to content

Commit 3a501a0

Browse files
committed
esp32s2: canio: respond to review comments
* explain the introduction of the temporary variable in get_t_config * get rid of unneeded __attribute__ * get rid of unneeded members of canio_can_obj_t * get rid of unneeded header inclusion
1 parent 2ba6659 commit 3a501a0

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

ports/esp32s2/common-hal/canio/CAN.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "shared-bindings/microcontroller/Pin.h"
3434
#include "shared-bindings/util.h"
3535
#include "supervisor/port.h"
36-
#include "hal/twai_ll.h"
3736

3837
#include "hal/twai_types.h"
3938

@@ -43,6 +42,10 @@ twai_timing_config_t get_t_config(int baudrate) {
4342
switch(baudrate) {
4443
case 1000000:
4544
{
45+
// TWAI_TIMING_CONFIG_abc expands to a C designated initializer list
46+
// { .brp = 4, ...}. This is only acceptable to the compiler as an
47+
// initializer and 'return TWAI_TIMING_CONFIG_1MBITS()` is not valid.
48+
// Instead, introduce a temporary, named variable and return it.
4649
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS();
4750
return t_config;
4851
}
@@ -116,7 +119,6 @@ twai_timing_config_t get_t_config(int baudrate) {
116119
}
117120
}
118121

119-
__attribute__((optimize("O0")))
120122
void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent)
121123
{
122124
#define DIV_ROUND(a, b) (((a) + (b)/2) / (b))
@@ -129,7 +131,7 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc
129131
mp_raise_ValueError(translate("loopback + silent mode not supported by peripheral"));
130132
}
131133

132-
self->t_config = get_t_config(baudrate);;
134+
twai_timing_config_t t_config = get_t_config(baudrate);
133135
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(-1, -1, TWAI_MODE_NORMAL);
134136
g_config.tx_io = tx->number;
135137
g_config.rx_io = rx->number;
@@ -139,14 +141,10 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc
139141
if (silent) {
140142
g_config.mode = TWAI_MODE_LISTEN_ONLY;
141143
}
142-
self->g_config = g_config;
143144

144-
{
145145
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
146-
self->f_config = f_config;
147-
}
148146

149-
esp_err_t result = twai_driver_install(&self->g_config, &self->t_config, &self->f_config);
147+
esp_err_t result = twai_driver_install(&g_config, &t_config, &f_config);
150148
if (result == ESP_ERR_NO_MEM) {
151149
mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
152150
} else if (result == ESP_ERR_INVALID_ARG) {

ports/esp32s2/common-hal/canio/CAN.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,4 @@ typedef struct canio_can_obj {
4646
bool silent:1;
4747
bool auto_restart:1;
4848
bool fifo_in_use:1;
49-
twai_filter_config_t f_config;
50-
twai_general_config_t g_config;
51-
twai_timing_config_t t_config;
5249
} canio_can_obj_t;

0 commit comments

Comments
 (0)