Skip to content

Commit

Permalink
feature: transmit path works (queue-less)
Browse files Browse the repository at this point in the history
  • Loading branch information
uhi22 committed Oct 19, 2023
1 parent 73fec9f commit 3cf7608
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ So we end-up with the following features for the moment:

The states and transitions of the ESP32S3 CAN driver are described here: https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/twai.html

### How did the CAN reception work (in the old world with espressif TWAI driver?
### How did the CAN reception work (in the old world with espressif TWAI driver)?

- During startup, the task task_LowLevelRX is started.
- The task_LowLevelRX suspends in twai_receive().
Expand All @@ -96,6 +96,16 @@ canBuses[i]->watchFor() --> setRXFilter()
CAN_COMMON::attachCANInterrupt() --> CAN0.setCallback() -->
...tbd...

### Where is the espressif TWAI driver stored?

Source code in the espressif IDE:
- C:\esp-idf-v4.4.4\components\driver\twai.c

Header and lib in the arduino IDE:
- C:\Users\uwemi\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\tools\sdk\esp32s3\include\driver\include\driver\twai.h
- C:\Users\uwemi\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\tools\sdk\esp32s3\lib\libdriver.a


### Is useBinarySerialComm true?

Yes, the SavvyCAN sends 0xE7 as first two bytes. This turns-on the binary mode.
Expand Down
38 changes: 34 additions & 4 deletions esp32_can_builtin_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ twai_filter_config_t twai_filters_cfg = TWAI_FILTER_CONFIG_ACCEPT_ALL();
#define TWAI_ISR_ATTR
#define TWAI_MALLOC_CAPS MALLOC_CAP_DEFAULT
#endif //CONFIG_TWAI_ISR_IN_IRAM

#define TWAI_CHECK(cond, ret_val) ({ \
if (!(cond)) { \
return (ret_val); \
} \
})

intr_handle_t experimental_isr_handle;
static twai_hal_context_t twai_context;
volatile uint32_t expCounterIsr;
Expand Down Expand Up @@ -400,19 +407,39 @@ void ESP32CAN::disable()
//twai_driver_uninstall();
}

esp_err_t my_twai_transmit(const twai_message_t *message) {
/* based on: C:\esp-idf-v4.4.4\components\driver\twai.c,
but without queue. This means: If the application sends
faster than the CAN is able to handle, the message will
be lost. A queue would not improve this situation long term. */

/* Check arguments */
TWAI_CHECK(message != NULL, ESP_ERR_INVALID_ARG);
TWAI_CHECK((message->data_length_code <= TWAI_FRAME_MAX_DLC) || message->dlc_non_comp, ESP_ERR_INVALID_ARG);

/* Format frame */
esp_err_t ret = ESP_FAIL;
twai_hal_frame_t tx_frame;
twai_hal_format_frame(message, &tx_frame);
/* try to send the frame immediately */
twai_hal_set_tx_buffer_and_transmit(&twai_context, &tx_frame);
ret = ESP_OK;
return ret;
}

bool ESP32CAN::sendFrame(CAN_FRAME& txFrame)
{
twai_message_t __TX_frame;
int returnCode;

__TX_frame.identifier = txFrame.id;
__TX_frame.data_length_code = txFrame.length;
__TX_frame.rtr = txFrame.rtr;
__TX_frame.extd = txFrame.extended;
for (int i = 0; i < 8; i++) __TX_frame.data[i] = txFrame.data.byte[i];

//don't wait long if the queue was full. The end user code shouldn't be sending faster
//than the buffer can empty. Set a bigger TX buffer or delay sending if this is a problem.
switch (twai_transmit(&__TX_frame, pdMS_TO_TICKS(4)))
returnCode = my_twai_transmit(&__TX_frame);
switch (returnCode)
{
case ESP_OK:
if (debuggingMode) Serial.write('<');
Expand All @@ -424,7 +451,10 @@ bool ESP32CAN::sendFrame(CAN_FRAME& txFrame)
case ESP_FAIL:
case ESP_ERR_INVALID_STATE:
case ESP_ERR_NOT_SUPPORTED:
if (debuggingMode) Serial.write('!');
if (debuggingMode) {
Serial.write('!');
Serial.print(returnCode);
}
break;
}

Expand Down
11 changes: 11 additions & 0 deletions savvycan_signalview_wifican_and_foccci.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SV1,1000,wificanStatistics1,wificanAliveCounter
SV1,1000,wificanStatistics1,wificanTotalRxMessages
SV1,1001,wificanStatistics2,wificanLostRxFrames
SV1,567,FOCCCI01,foccci_uptime_s
SV1,567,FOCCCI01,foccciCheckpoint
SV1,568,foccciFast01,EVSEPresentVoltage
SV1,568,foccciFast01,uCcsInlet_V
SV1,569,foccciTemperatures01,TempCPU
SV1,569,foccciTemperatures01,Temp1
SV1,569,foccciTemperatures01,Temp2
SV1,569,foccciTemperatures01,Temp3

0 comments on commit 3cf7608

Please sign in to comment.