rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
can_twai.c File Reference

The panel's TWAI controller on the link: start, stop, send and receive. More...

#include "can_twai.h"
#include <string.h>
#include "driver/twai.h"
#include "esp_idf_version.h"
#include "esp_log.h"
#include "board_pins.h"
#include "can_timing.h"

Functions

esp_err_t can_twai_start (uint32_t bitrate)
 Start TWAI at a bit rate made exactly from the 40MHz crystal.
void can_twai_stop (void)
 Stop TWAI and uninstall its driver. Does nothing when it is not running.
bool can_twai_send (const link_can_frame_t *f, uint32_t timeout_ms)
bool can_twai_recv (link_can_frame_t *f, uint32_t timeout_ms)
bool can_twai_errors (uint32_t *tx_err, uint32_t *rx_err, uint32_t *bus_err, bool *bus_off)
can_twai_health_t can_twai_health (void)
can_twai_health_t can_twai_recover (void)
 Look at the controller and put it back on the bus if it has fallen off.

Variables

static const char * TAG = "can"
static bool s_running

Detailed Description

The panel's TWAI controller on the link: start, stop, send and receive.

SPDX-License-Identifier: MIT

Function Documentation

◆ can_twai_errors()

bool can_twai_errors ( uint32_t * tx_err,
uint32_t * rx_err,
uint32_t * bus_err,
bool * bus_off )

Bus error counters, for the report. Any pointer may be NULL.

False when the controller is not running or its status would not read; the outputs are then left as the caller set them, because zeros would read as a healthy bus.

◆ can_twai_health()

can_twai_health_t can_twai_health ( void )

The same reading without acting on it.

For anything that reports the controller's state rather than repairing it, so a diagnosis does not start a recovery as a side effect of being looked at.

◆ can_twai_recover()

can_twai_health_t can_twai_recover ( void )

Look at the controller and put it back on the bus if it has fallen off.

A transmitter nobody answers reaches the bus-off threshold in about four milliseconds at 1Mbit/s, and this peripheral does not recover on its own: without this call every later transmit fails and the link is gone until the panel is power-cycled. Call it while the link is down; it takes one step per call and never blocks.

◆ can_twai_recv()

bool can_twai_recv ( link_can_frame_t * f,
uint32_t timeout_ms )

Take one frame, if one arrives within timeout_ms.

◆ can_twai_send()

bool can_twai_send ( const link_can_frame_t * f,
uint32_t timeout_ms )

Queue one frame. timeout_ms of 0 does not block.

◆ can_twai_start()

esp_err_t can_twai_start ( uint32_t bitrate)

Start TWAI at a bit rate made exactly from the 40MHz crystal.

Parameters
bitrateBits per second; PANEL_CAN_BITRATE on the link.
Returns
ESP_OK; ESP_ERR_INVALID_ARG for a rate the crystal does not make exactly; otherwise the TWAI driver's error, with the driver uninstalled again.