Skip to content

Canbus: api changes #3466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8b82c23
canio: doc improvements
jepler Sep 24, 2020
79ca430
Match: address -> id
jepler Sep 24, 2020
48bda58
Listerner: read -> receive, drop readinto
jepler Sep 24, 2020
8d45be1
canio: Split RemoteTransmissionRequest to its own class
jepler Sep 24, 2020
f4fd5bb
canio: docs: fix how we refer to 'the listen method'
jepler Sep 26, 2020
ea2f5b6
canio: Correct type annotations of CAN.send, Listener.receive
jepler Sep 26, 2020
3e97e9c
canio: Listener: shuffle function declarations around
jepler Sep 26, 2020
a4cc3ad
canio: RemoteTransmissionRequest: Split implementation, keep one stru…
jepler Sep 26, 2020
4f7f1e8
canio: CAN.listen: rename argument to 'matches', reflecting that it i…
jepler Sep 28, 2020
1349373
canio: CAN.Match: improve how an unspecified `mask` is implemented
jepler Sep 28, 2020
f4e36fc
CAN: Use mp_obj_t insteaed of canio_message_obj_t, get rid of rtr field
jepler Sep 28, 2020
04e434a
canio: remove stray remote_transmission_request method declaration
jepler Sep 28, 2020
809225f
make translate
jepler Sep 28, 2020
979ec3a
can: RemoteTransmissionRequest: remove duplicate docstrings
jepler Sep 29, 2020
03bd968
more doc improvements
jepler Sep 29, 2020
1bea099
Allow the _state_count properties to be unimplemented
jepler Sep 29, 2020
611f81a
canio: actually drop the _error_count properties
jepler Sep 29, 2020
d79f4e2
canio: doc improvement -- don't needlessly call out deinit behavior
jepler Sep 29, 2020
88cbf77
canio.CAN: clarify read-only status of several properties
jepler Sep 30, 2020
c129c8f
Merge remote-tracking branch 'origin/main' into canbus-api-changes
jepler Sep 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-29 11:11+0530\n"
"POT-Creation-Date: 2020-09-29 20:14-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -1530,6 +1530,10 @@ msgstr ""
msgid "Refresh too soon"
msgstr ""

#: shared-bindings/canio/RemoteTransmissionRequest.c
msgid "RemoteTransmissionRequests limited to 8 bytes"
msgstr ""

#: shared-bindings/aesio/aes.c
msgid "Requested AES mode is unsupported"
msgstr ""
Expand Down Expand Up @@ -2412,10 +2416,14 @@ msgstr ""
msgid "exceptions must derive from BaseException"
msgstr ""

#: shared-bindings/canio/CAN.c shared-bindings/canio/Listener.c
#: shared-bindings/canio/CAN.c
msgid "expected '%q' but got '%q'"
msgstr ""

#: shared-bindings/canio/CAN.c
msgid "expected '%q' or '%q' but got '%q'"
msgstr ""

#: py/objstr.c
msgid "expected ':' after format specifier"
msgstr ""
Expand Down Expand Up @@ -3275,10 +3283,6 @@ msgstr ""
msgid "source palette too large"
msgstr ""

#: shared-bindings/canio/Message.c
msgid "specify size or data, but not both"
msgstr ""

#: py/objstr.c
msgid "start/end indices"
msgstr ""
Expand Down
34 changes: 5 additions & 29 deletions ports/atmel-samd/common-hal/canio/CAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,6 @@ int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self)
return self->hw->ECR.bit.REC;
}

int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self)
{
return self->error_warning_state_count;
}

int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self)
{
return self->error_passive_state_count;
}

int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self)
{
return self->bus_off_state_count;
}

canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self) {
CAN_PSR_Type psr = self->hw->PSR;
if (psr.bit.BO) {
Expand Down Expand Up @@ -328,16 +313,18 @@ static void maybe_auto_restart(canio_can_obj_t *self) {
}
}

void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message)
void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in)
{
maybe_auto_restart(self);

canio_message_obj_t *message = message_in;;
// We have just one dedicated TX buffer, use it!
canio_can_tx_buffer_t *ent = &self->state->tx_buffer[0];

bool rtr = message->base.type == &canio_remote_transmission_request_type;
ent->txb0.bit.ESI = false;
ent->txb0.bit.XTD = message->extended;
ent->txb0.bit.RTR = message->rtr;
ent->txb0.bit.RTR = rtr;
if (message->extended) {
ent->txb0.bit.ID = message->id;
} else {
Expand All @@ -350,7 +337,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *messa
ent->txb1.bit.BRS = 0; // No bit rate switching
ent->txb1.bit.DLC = message->size;

if (!message->rtr) {
if (!rtr) {
memcpy(ent->data, message->data, message->size);
}

Expand Down Expand Up @@ -417,17 +404,6 @@ STATIC void can_handler(int i) {
Can *hw = can_insts[i];
uint32_t ir = hri_can_read_IR_reg(hw);

/* Count up errors*/
if (ir & CAN_IE_EWE) {
self->error_warning_state_count += 1;
}
if (ir & CAN_IE_EPE) {
self->error_passive_state_count += 1;
}
if (ir & CAN_IE_BOE) {
self->bus_off_state_count += 1;
}

/* Acknowledge interrupt */
hri_can_write_IR_reg(hw, ir);
}
Expand Down
73 changes: 38 additions & 35 deletions ports/atmel-samd/common-hal/canio/Listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "common-hal/canio/__init__.h"
#include "common-hal/canio/Listener.h"
#include "shared-bindings/canio/Listener.h"
#include "shared-bindings/util.h"
#include "supervisor/shared/tick.h"
#include "component/can.h"
Expand All @@ -58,8 +59,8 @@ STATIC void static_assertions(void) {
MP_STATIC_ASSERT(CAN_XIDFE_0_EFEC_STF0M_Val + 1 == CAN_XIDFE_0_EFEC_STF1M_Val);
}

STATIC bool single_address_filter(canio_match_obj_t *match) {
return match->mask == 0 || match->mask == match->address;
STATIC bool single_id_filter(canio_match_obj_t *match) {
return match->mask == 0 || match->mask == match->id;
}

STATIC bool standard_filter_in_use(CanMramSidfe *filter) {
Expand All @@ -76,7 +77,7 @@ STATIC size_t num_filters_needed(size_t nmatch, canio_match_obj_t **matches, boo
if (extended != matches[i]->extended) {
continue;
}
if (single_address_filter(matches[i])) {
if (single_id_filter(matches[i])) {
num_half_filters_needed += 1;
} else {
num_half_filters_needed += 2;
Expand Down Expand Up @@ -191,7 +192,7 @@ STATIC void install_extended_filter(CanMramXidfe *extended, int id1, int id2, in
}


#define NO_ADDRESS (-1)
#define NO_ID (-1)
void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **matches) {
int fifo = self->fifo_idx;

Expand All @@ -207,31 +208,31 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
CanMramSidfe *standard = next_standard_filter(self, NULL);
CanMramXidfe *extended = next_extended_filter(self, NULL);

int first_address = NO_ADDRESS;
int first_id = NO_ID;

// step 1: single address standard matches
// step 1: single id standard matches
// we have to gather up pairs and stuff them in a single filter entry
for(size_t i = 0; i<nmatch; i++) {
canio_match_obj_t *match = matches[i];
if (match->extended) {
continue;
}
if (!single_address_filter(match)) {
if (!single_id_filter(match)) {
continue;
}
if (first_address != NO_ADDRESS) {
install_standard_filter(standard, first_address, match->address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
first_address = NO_ADDRESS;
if (first_id != NO_ID) {
install_standard_filter(standard, first_id, match->id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
first_id = NO_ID;
standard = next_standard_filter(self, standard);
} else {
first_address = match->address;
first_id = match->id;
}
}
// step 1.5. odd single address standard match
if (first_address != NO_ADDRESS) {
install_standard_filter(standard, first_address, first_address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
// step 1.5. odd single id standard match
if (first_id != NO_ID) {
install_standard_filter(standard, first_id, first_id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
standard = next_standard_filter(self, standard);
first_address = NO_ADDRESS;
first_id = NO_ID;
}

// step 2: standard mask filter
Expand All @@ -240,36 +241,36 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
if (match->extended) {
continue;
}
if (single_address_filter(match)) {
if (single_id_filter(match)) {
continue;
}
install_standard_filter(standard, match->address, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val);
install_standard_filter(standard, match->id, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val);
standard = next_standard_filter(self, standard);
}

// step 3: single address extended matches
// step 3: single id extended matches
// we have to gather up pairs and stuff them in a single filter entry
for(size_t i = 0; i<nmatch; i++) {
canio_match_obj_t *match = matches[i];
if (!match->extended) {
continue;
}
if (!single_address_filter(match)) {
if (!single_id_filter(match)) {
continue;
}
if (first_address != NO_ADDRESS) {
install_extended_filter(extended, first_address, match->address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
first_address = NO_ADDRESS;
if (first_id != NO_ID) {
install_extended_filter(extended, first_id, match->id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
first_id = NO_ID;
extended = next_extended_filter(self, extended);
} else {
first_address = match->address;
first_id = match->id;
}
}
// step 3.5. odd single address standard match
if (first_address != NO_ADDRESS) {
install_extended_filter(extended, first_address, first_address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
// step 3.5. odd single id standard match
if (first_id != NO_ID) {
install_extended_filter(extended, first_id, first_id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
extended = next_extended_filter(self, extended);
first_address = NO_ADDRESS;
first_id = NO_ID;
}

// step 4: extended mask filters
Expand All @@ -278,10 +279,10 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
if (!match->extended) {
continue;
}
if (single_address_filter(match)) {
if (single_id_filter(match)) {
continue;
}
install_extended_filter(extended, match->address, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val);
install_extended_filter(extended, match->id, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val);
extended = next_extended_filter(self, extended);
}

Expand Down Expand Up @@ -348,30 +349,32 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) {
return self->hw->RXFS.bit.F0FL;
}

bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_message_obj_t *message) {
mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self) {
if (!common_hal_canio_listener_in_waiting(self)) {
uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms;
do {
if (supervisor_ticks_ms64() > deadline) {
return false;
return NULL;
}
} while (!common_hal_canio_listener_in_waiting(self));
}
int index = self->hw->RXFS.bit.F0GI;
canio_can_rx_fifo_t *hw_message = &self->fifo[index];
bool rtr = hw_message->rxf0.bit.RTR;
canio_message_obj_t *message = m_new_obj(canio_message_obj_t);
message->base.type = rtr ? &canio_remote_transmission_request_type : &canio_message_type;
message->extended = hw_message->rxf0.bit.XTD;
if (message->extended) {
message->id = hw_message->rxf0.bit.ID;
} else {
message->id = hw_message->rxf0.bit.ID >> 18; // short addresses are left-justified
message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified
}
message->rtr = hw_message->rxf0.bit.RTR;
message->size = hw_message->rxf1.bit.DLC;
if (!message->rtr) {
if (!rtr) {
memcpy(message->data, hw_message->data, message->size);
}
self->hw->RXFA.bit.F0AI = index;
return true;
return message;
}

void common_hal_canio_listener_deinit(canio_listener_obj_t *self) {
Expand Down
10 changes: 1 addition & 9 deletions ports/atmel-samd/common-hal/canio/Listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,11 @@ typedef struct {
__IO CAN_RXF0A_Type RXFA; /**< \brief (R/W 32) Rx FIFO n Acknowledge */
} canio_rxfifo_reg_t;

typedef struct {
typedef struct canio_listener_obj {
mp_obj_base_t base;
canio_can_obj_t *can;
canio_can_rx_fifo_t *fifo;
canio_rxfifo_reg_t *hw;
uint32_t timeout_ms;
uint8_t fifo_idx;
} canio_listener_obj_t;

void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout);
void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self);
void common_hal_canio_listener_deinit(canio_listener_obj_t *self);
bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_message_obj_t *message);
int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self);
float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self);
void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout);
1 change: 1 addition & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ SRC_SHARED_MODULE_ALL = \
_bleio/ScanResults.c \
canio/Match.c \
canio/Message.c \
canio/RemoteTransmissionRequest.c \
_eve/__init__.c \
_pixelbuf/PixelBuf.c \
_pixelbuf/__init__.c \
Expand Down
Loading