Post · 3 min read· pp. 98–101

CAN Controller Driver function reference

pic18f26k83_can.h

Functions

void pic18f26k83_can_init(const can_timing_t *timing, void (*receive_callback)(const can_msg_t *message)) : Initialize PIC18F26K83 CAN Controller.

code
Warning

Note that this function DOES NOT setup the inputs and outputs from the CAN module to the output pins, application code must do that. In order to do that, CANRXPPS must be set to the proper pin value for the CANRX pin, and \_\_\_PPS must be set to 0x33 to mark it as outputting from the CAN module. In addition, TRIS and ANSEL registers for whatever pin is being used must be set to the right values.

Parameters:
:   - **timing** – Timing parameters generated by `can_generate_timing_params` function
    - **receive\_callback** – CAN message receive handler, the function would be called when a CAN message is received

void pic18f26k83_can_send(const can_msg_t *message) : Send a CAN message.

code
Parameters:
:   **message** – CAN message to be sent

bool pic18f26k83_can_send_rdy(void) : Check if CAN controller is ready to send a message.

code
Returns:
:   return `true` if ready to send a message

void pic18f26k83_can_handle_interrupt(void) : Call this function in an interrupt context whenever PIR5 is nonzero.

code
Note

This function clears PIR5 interrupt flags

pic18f26k83_can_timing_util.h

Defines

CAN_BIT_TIME_US

Functions

w_status_t pic18f26k83_can_generate_timing_params(uint32_t system_freq, can_timing_t *timing) : Generate CAN bus timing setting for PIC microcontrollers.

code
Parameters:
:   - **system\_freq** – PIC Fosc frequency in Hz
    - **timing** – buffer to write timing parameters to

Returns:
:   `W_SUCCESS` if system\_freq is valid and parameters have been written to `timing`

struct can_timing_t : Public Members

code
uint8\_t brp

uint8\_t sjw

uint8\_t sam

uint8\_t seg1ph

uint8\_t seg2ph

uint8\_t prseg

bool btlmode

stm32_can.h

Functions

bool stm32_can_init(FDCAN_HandleTypeDef *handle, void (*receive_callback)(const can_msg_t *message)) : Start STM32 CAN Controller.

code
Note

This depends on STM32CubeMX generated HAL code has already initialized CAN controller

Parameters:
:   - **handle** – pointer to FDCAN\_HandleTypeDef struct in `main.c`
    - **receive\_callback** – CAN message receive handler, the function would be called when a CAN message is received

Returns:
:   `true` if succeed

bool stm32_can_send(const can_msg_t *message) : Send a CAN message.

code
Parameters:
:   **message** – CAN message to be sent

Returns:
:   `true` if succeed

bool stm32_can_send_rdy(void) : Check if CAN controller is ready to send a message.

code
Returns:
:   `true` if ready to send a message

mcp2515.h

Functions

void mcp2515_can_init(can_timing_t *timing, uint8_t (*spi_read_fcn)(void), void (*spi_write_fcn)(uint8_t data), void (*cs_drive_fcn)(uint8_t state)) : Initialize MCP2515 CAN Controller.

code
Parameters:
:   - **timing** – Timing parameters generated by `can_generate_timing_params` function
    - **spi\_read\_fcn** – Function to read 8 bits from SPI bus
    - **spi\_write\_fcn** – Function to write 8 bits to SPI bus
    - **cs\_drive\_fcn** – Function to drive SPI Chip Select signal to `state` logic level

void mcp2515_can_send(can_msg_t *message) : Send a CAN message.

code
Parameters:
:   **message** – CAN message to be sent

bool mcp2515_can_send_rdy(void) : Check if CAN controller is ready to send a message.

code
Returns:
:   return `true` if ready to send a message

bool mcp2515_can_receive(can_msg_t *message) : Attempt to receive a CAN message.

code
Parameters:
:   **message** – CAN message receive buffer

Returns:
:   return `true` if a message is received