CAN CoreX 2.3.1
Table-driven CAN, CAN FD, ISO-TP, network simulation, and bus monitoring library
Loading...
Searching...
No Matches
Core CAN

Table-driven CAN RX/TX, queue management, timebase, statistics, and CAN FD helpers. More...

Data Structures

struct  CCX_GlobalStats_t
 Global statistics for CAN instance. More...
struct  CCX_message_t
 Structure representing a CAN message. More...
struct  CCX_RX_table_t
 CAN RX table entry structure. More...
struct  CCX_TX_table_t
 CAN TX table entry structure. More...
struct  CCX_instance_t

Macros

#define CCX_TICK_FROM_FUNC   0
 Enables system tick retrieval via a function call.
#define CCX_HR_TICK_FROM_FUNC   0
 Enables high-resolution tick retrieval via a function call.
#define CCX_RX_BUFFER_SIZE   48
 Size of the CAN receive buffer.
#define CCX_TX_BUFFER_SIZE   48
 Size of the CAN transmit buffer.
#define CCX_ENABLE_CANFD   1
 Enables CAN FD support (64-byte payload, BRS, ESI, FDF).
#define CCX_DLC_ANY   16
 Special DLC value for wildcard matching in RX table.
#define CCX_RX_HASH_SIZE   64
 Size of the hash table for RX message lookup (used when CCX_RX_SEARCH_HASH is defined).
#define CCX_TIME_BASE_SCALAR   uint32_t
#define CCX_MAX_TIMEOUT   UINT32_MAX
#define CCX_HR_TIME_BASE_SCALAR   uint16_t
#define CCX_HR_MAX_TIMEOUT   UINT16_MAX
#define CCX_INTERNAL_MS_FROM_US_CEIL(us)
#define CCX_HR_TIME(us)
#define CCX_GetPrimaryTick()
#define CCX_GetHighResTick()

Typedefs

typedef volatile uint32_t CCX_TIME_t
typedef volatile uint16_t CCX_HR_TIME_t
typedef struct CCX_instance_t CCX_instance_t

Enumerations

enum  CCX_BusIsFree_t { CCX_BUS_BUSY = 0 , CCX_BUS_FREE }
 Enumeration indicating the status of the CAN bus. More...
enum  CCX_ide_t { CCX_ID_STANDARD = 0 , CCX_ID_EXTENDED = 1 }
 CAN identifier type (standard 11-bit or extended 29-bit). More...
enum  CCX_frame_format_t { CCX_FRAME_FORMAT_CLASSIC = 0 , CCX_FRAME_FORMAT_FD = 1 , CCX_FRAME_FORMAT_FD_BRS = 2 }
 CAN frame format selection. More...
enum  CCX_MsgRegStatus_t { CCX_MSG_UNREG , CCX_MSG_REG }
enum  CCX_Status_t {
  CCX_OK = 0 , CCX_NULL_PTR , CCX_WRONG_ARG , CCX_BUS_TOO_BUSY ,
  CCX_MISSING_TIMEBASE
}
enum  CCX_FD_DLC_t {
  CCX_FD_DLC_0B = 0 , CCX_FD_DLC_1B = 1 , CCX_FD_DLC_2B = 2 , CCX_FD_DLC_3B = 3 ,
  CCX_FD_DLC_4B = 4 , CCX_FD_DLC_5B = 5 , CCX_FD_DLC_6B = 6 , CCX_FD_DLC_7B = 7 ,
  CCX_FD_DLC_8B = 8 , CCX_FD_DLC_12B = 9 , CCX_FD_DLC_16B = 10 , CCX_FD_DLC_20B = 11 ,
  CCX_FD_DLC_24B = 12 , CCX_FD_DLC_32B = 13 , CCX_FD_DLC_48B = 14 , CCX_FD_DLC_64B = 15
}
 Named CAN FD DLC values. More...

Functions

CCX_Status_t CCX_RX_PushMsg (CCX_instance_t *Instance, const CCX_message_t *msg)
CCX_Status_t CCX_TX_PushMsg (CCX_instance_t *Instance, const CCX_message_t *msg)
uint16_t CCX_RX_GetDepth (const CCX_instance_t *Instance)
uint16_t CCX_TX_GetDepth (const CCX_instance_t *Instance)
uint16_t CCX_RX_GetFree (const CCX_instance_t *Instance)
uint16_t CCX_TX_GetFree (const CCX_instance_t *Instance)
void CCX_FlushRx (CCX_instance_t *Instance)
void CCX_FlushTx (CCX_instance_t *Instance)
void CCX_Flush (CCX_instance_t *Instance)
void CCX_Reset (CCX_instance_t *Instance)
CCX_Status_t CCX_Poll (CCX_instance_t *Instance)
CCX_Status_t CCX_Init (CCX_instance_t *Instance, CCX_RX_table_t *CCX_RX_table, CCX_TX_table_t *CCX_TX_table, uint16_t RxTableSize, uint16_t TxTableSize, void(*SendFunction)(const CCX_instance_t *Instance, const CCX_message_t *msg), CCX_BusIsFree_t(*BusCheck)(const CCX_instance_t *Instance), void(*ParserUnregMsg)(const CCX_instance_t *Instance, CCX_message_t *Msg))
void CCX_tick_variable_register (CCX_TIME_t *Variable)
 Registers the system tick source variable.
void CCX_high_res_tick_variable_register (CCX_HR_TIME_t *Variable)
uint8_t CCX_IsPrimaryTickRegistered (void)
uint8_t CCX_IsHighResTickRegistered (void)
const CCX_GlobalStats_tCCX_GetGlobalStats (const CCX_instance_t *Instance)
 Get global statistics.
void CCX_ResetGlobalStats (CCX_instance_t *Instance)
 Reset global statistics.
void CCX_OnMessageTransmitted (CCX_instance_t *Instance, const CCX_message_t *msg)
 Notify library that a message was successfully transmitted.
void CCX_RX_RebuildHash (CCX_instance_t *Instance)
 Rebuild hash table for RX message lookup.
uint8_t CCX_FD_LenToDLC (uint8_t len)
 Convert byte length to the smallest valid CAN FD DLC.

Variables

CCX_TIME_t * CCX_tick
CCX_HR_TIME_t * CCX_high_res_tick
const uint8_t CCX_FD_DLC_TO_LEN [16]
 DLC-to-payload-length lookup table: index 0-15, values {0,1,2,3,4,5,6,7,8,12,16,20,24,32,48,64}.

Detailed Description

Table-driven CAN RX/TX, queue management, timebase, statistics, and CAN FD helpers.

Macro Definition Documentation

◆ CCX_DLC_ANY

#define CCX_DLC_ANY   16

Special DLC value for wildcard matching in RX table.

When set in CCX_RX_table_t.DLC the parser is called for any received DLC. In classic builds (CCX_ENABLE_CANFD=0) the sentinel is 15 (fits in 4-bit DLC field, classic frames are DLC 0-8). In FD builds (CCX_ENABLE_CANFD=1) the sentinel is 16 (max valid FD DLC is 15, stored in the 5-bit DLC field). FrameFormat in the table entry controls which frame format is matched: CLASSIC → classic wildcard, FD/FD_BRS → FD wildcard.

Example (FD build): CCX_RX_table_t rx_table[] = { {.ID = 0x100, .DLC = 8, .FrameFormat = CCX_FRAME_FORMAT_CLASSIC, .Parser = exact_parser}, {.ID = 0x200, .DLC = CCX_DLC_ANY, .FrameFormat = CCX_FRAME_FORMAT_FD, .Parser = fd_any_parser}, };

◆ CCX_ENABLE_CANFD

#define CCX_ENABLE_CANFD   1

Enables CAN FD support (64-byte payload, BRS, ESI, FDF).

When 0 (default): zero RAM/code overhead, ABI identical to v1.x. When 1: CCX_message_t grows to 64-byte payload; CCX_frame_format_t and CCX_ide_t enums available.

◆ CCX_GetHighResTick

#define CCX_GetHighResTick ( )
Value:
((CCX_high_res_tick != NULL) ? (*CCX_high_res_tick) : ((CCX_HR_TIME_t)0))

◆ CCX_GetPrimaryTick

#define CCX_GetPrimaryTick ( )
Value:
((CCX_tick != NULL) ? (*CCX_tick) : ((CCX_TIME_t)0))

◆ CCX_HR_TICK_FROM_FUNC

#define CCX_HR_TICK_FROM_FUNC   0

Enables high-resolution tick retrieval via a function call.

This switch is independent from CCX_TICK_FROM_FUNC.

◆ CCX_HR_TIME

#define CCX_HR_TIME ( us)
Value:
((CCX_HR_TIME_t)(us))

◆ CCX_INTERNAL_MS_FROM_US_CEIL

#define CCX_INTERNAL_MS_FROM_US_CEIL ( us)
Value:
(((us) == 0U) ? 0U : (((us)-1U) / 1000U) + 1U)

◆ CCX_RX_BUFFER_SIZE

#define CCX_RX_BUFFER_SIZE   48

Size of the CAN receive buffer.

Defines the number of messages that can be stored in the CAN RX buffer.

◆ CCX_RX_HASH_SIZE

#define CCX_RX_HASH_SIZE   64

Size of the hash table for RX message lookup (used when CCX_RX_SEARCH_HASH is defined).

Default value is 64. Can be overridden before including this header. Larger values reduce collisions but increase memory usage. Recommended to use a power of 2 or prime number.

◆ CCX_TICK_FROM_FUNC

#define CCX_TICK_FROM_FUNC   0

Enables system tick retrieval via a function call.

If set to 1, the system time base is obtained by calling a function that returns the current tick count. If set to 0, the system time base is read directly from a variable.

◆ CCX_TX_BUFFER_SIZE

#define CCX_TX_BUFFER_SIZE   48

Size of the CAN transmit buffer.

Defines the number of messages that can be stored in the CAN TX buffer.

Enumeration Type Documentation

◆ CCX_BusIsFree_t

Enumeration indicating the status of the CAN bus.

This enum represents whether the CAN bus is free for transmitting a new message or if it is currently busy.

Values:

  • CCX_BUS_BUSY: The CAN bus is currently busy and cannot accept new messages.
  • CCX_BUS_FREE: The CAN bus is free and ready for new message transmission.

◆ CCX_FD_DLC_t

Named CAN FD DLC values.

Use these when setting the DLC field of CCX_message_t or CCX_TX_table_t to make the intended payload length explicit.

◆ CCX_frame_format_t

CAN frame format selection.

Used in message, RX table, and TX table entries to specify the frame format. A single 2-bit field replaces the separate FDF and BRS bitfields from v1.x.

For RX table entries:

  • CCX_FRAME_FORMAT_CLASSIC: entry matches only classic (non-FD) frames
  • CCX_FRAME_FORMAT_FD / CCX_FRAME_FORMAT_FD_BRS: entry matches only FD frames

For TX table / message entries:

  • Controls the frame format transmitted on the bus
Enumerator
CCX_FRAME_FORMAT_CLASSIC 

Classic CAN 2.0, max 8-byte payload.

CCX_FRAME_FORMAT_FD 

CAN FD without bit-rate switch (BRS=0).

CCX_FRAME_FORMAT_FD_BRS 

CAN FD with bit-rate switch (BRS=1).

◆ CCX_ide_t

enum CCX_ide_t

CAN identifier type (standard 11-bit or extended 29-bit).

Enumerator
CCX_ID_STANDARD 

Standard 11-bit identifier.

CCX_ID_EXTENDED 

Extended 29-bit identifier.

Function Documentation

◆ CCX_FD_LenToDLC()

uint8_t CCX_FD_LenToDLC ( uint8_t len)

Convert byte length to the smallest valid CAN FD DLC.

Rounds up to the nearest valid FD payload length. Lengths > 64 return DLC 15 (64 bytes).

Parameters
lenDesired payload length in bytes
Returns
CAN FD DLC value (0-15)

◆ CCX_GetGlobalStats()

const CCX_GlobalStats_t * CCX_GetGlobalStats ( const CCX_instance_t * Instance)

Get global statistics.

Global statistics are always enabled and track basic operational metrics.

Parameters
InstanceCAN instance
Returns
Pointer to global statistics structure (always available, never NULL)

◆ CCX_OnMessageTransmitted()

void CCX_OnMessageTransmitted ( CCX_instance_t * Instance,
const CCX_message_t * msg )

Notify library that a message was successfully transmitted.

Call this function from your CAN TX complete interrupt handler. Automatically increments GlobalStats.total_tx_messages and calls OnMessageTransmitted callback if set.

Parameters
InstanceCAN instance
msgPointer to transmitted message (optional, can be NULL)
Note
This is the ONLY way to properly track transmitted messages
OnMessageTransmitted callback is for user notification only
// In STM32 HAL
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) {
CCX_OnMessageTransmitted(&can_instance, &last_sent_msg);
}
// In bare-metal ISR
void CAN1_TX_IRQHandler(void) {
if (CAN1->TSR & CAN_TSR_RQCP0) {
CAN1->TSR |= CAN_TSR_RQCP0;
CCX_OnMessageTransmitted(&can_instance, NULL);
}
}
void CCX_OnMessageTransmitted(CCX_instance_t *Instance, const CCX_message_t *msg)
Notify library that a message was successfully transmitted.

◆ CCX_ResetGlobalStats()

void CCX_ResetGlobalStats ( CCX_instance_t * Instance)

Reset global statistics.

Resets all global counters to zero.

Parameters
InstanceCAN instance

◆ CCX_RX_RebuildHash()

void CCX_RX_RebuildHash ( CCX_instance_t * Instance)

Rebuild hash table for RX message lookup.

This function rebuilds the internal hash table used for fast RX message lookup when CCX_RX_SEARCH_HASH is defined at compile time.

Call this function after modifying the RX table (CCX_RX_table) to update the hash table. The hash table is automatically built during CCX_Init(), so manual rebuild is only needed if you dynamically modify the RX table after initialization.

Parameters
InstanceCAN instance
Note
This function does nothing if CCX_RX_SEARCH_HASH is not defined
Not needed for linear search or binary search modes
// After modifying RX table
rx_table[5].ID = 0x456;
CCX_RX_RebuildHash(&can_instance);
void CCX_RX_RebuildHash(CCX_instance_t *Instance)
Rebuild hash table for RX message lookup.

◆ CCX_tick_variable_register()

void CCX_tick_variable_register ( CCX_TIME_t * Variable)

Registers the system tick source variable.

When CCX_TICK_FROM_FUNC is set to 0, this function registers a pointer to a variable that holds the current system tick value.

Parameters
VariablePointer to a volatile variable of type CCX_TIME_t representing the system tick.