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

The panel's CAN (Controller Area Network) driver. More...

#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
#include "link_can.h"

Go to the source code of this file.

Enumerations

enum  can_twai_health_t {
  CAN_TWAI_UNKNOWN = 0 , CAN_TWAI_RUNNING , CAN_TWAI_RECOVERING , CAN_TWAI_STOPPED ,
  CAN_TWAI_BUS_OFF
}

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)
can_twai_health_t can_twai_recover (void)
 Look at the controller and put it back on the bus if it has fallen off.
can_twai_health_t can_twai_health (void)
bool can_twai_errors (uint32_t *tx_err, uint32_t *rx_err, uint32_t *bus_err, bool *bus_off)

Detailed Description

The panel's CAN (Controller Area Network) driver.

On the ESP32-P4's TWAI (Two-Wire Automotive Interface) controller 0, GPIO22 TX and GPIO21 RX (board_pins.h), clocked from the 40MHz crystal. The bit timing comes from shared/can, which is host-tested; this file holds the peripheral's configuration and the send and receive calls.

SPDX-License-Identifier: MIT

Enumeration Type Documentation

◆ can_twai_health_t

What the controller is doing, as can_twai_recover() found it.

Enumerator
CAN_TWAI_UNKNOWN 

not started, or the status would not read

CAN_TWAI_RUNNING 

on the bus

CAN_TWAI_RECOVERING 

counting the bus-free signals before stopping

CAN_TWAI_STOPPED 

idle, and would not start

CAN_TWAI_BUS_OFF 

off the bus, and recovery would not begin

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.