piMCP2515
A library for controlling an MCP2515 on the Raspberry Pi and Raspberry Pi Pico
Loading...
Searching...
No Matches
Init and Config Functions

These functions handle initialization and configuration. More...

Functions

int mcp2515_bitrate_simplified (pi_mcp2515_t *pi_mcp2515, uint16_t baud_rate_kbps)
 A simplified bitrate setting function.
int mcp2515_bitrate_full_optional (pi_mcp2515_t *pi_mcp2515, uint16_t baud_rate_kbps, uint8_t sjw, uint8_t prescaler, uint8_t prseg_tqps, uint8_t phseg_tqps1, uint8_t phseg_tqps2, bool sof, bool wakfil, bool sam, bool btlmode)
 Configure the bitrate and all other CNF register stored parameters.
int mcp2515_cnf_set (pi_mcp2515_t *pi_mcp2515, uint8_t cnf1, uint8_t cnf2, uint8_t cnf3)
 Set the values of all CNF register.
uint8_t mcp2515_cnf_get (pi_mcp2515_t *pi_mcp2515, uint8_t cnf)
 Get the value of a CNF register.
int mcp2515_init (pi_mcp2515_t **pi_mcp2515, uint8_t spi_channel, uint8_t tx_pin, uint8_t rx_pin, uint8_t sck_pin, uint8_t cs_pin, uint32_t spi_clock, uint8_t osc_mhz)
 Setup and prepare a pi_mcp2515_t structure based on the parameters provided.
void mcp2515_conf_spi_devpath (pi_mcp2515_t *pi_mcp2515, char *spidev_path)
 Override the default path for the spi device (ex /dev/spi0).
void mcp2515_conf_gpio_devpath (pi_mcp2515_t *pi_mcp2515, char *gpiodev_path)
 Override the default path for the gpio device (ex /dev/gpio0).
void mcp2515_free (pi_mcp2515_t *pi_mcp2515)
 Cleanup after everything.

Detailed Description

These functions handle initialization and configuration.

Function Documentation

◆ mcp2515_bitrate_full_optional()

int mcp2515_bitrate_full_optional ( pi_mcp2515_t * pi_mcp2515,
uint16_t baud_rate_kbps,
uint8_t sjw,
uint8_t prescaler,
uint8_t prseg_tqps,
uint8_t phseg_tqps1,
uint8_t phseg_tqps2,
bool sof,
bool wakfil,
bool sam,
bool btlmode )

Configure the bitrate and all other CNF register stored parameters.

This function accepts all possible configurable options, while mcp2515_bitrate_simplified can be used for a simplified process.

TODO first draft. Test/validate/fix. Make sure this is right.

Parameters
pi_mcp2515the piMCP2515 handle.
baud_rate_kbpsthe baud rate to use in kbps. Commonly either 500 or 1000.
sjwthe synchronization jump width in Tqs (1-4).
prescalerthe prescaler divisor (0-63)
prseg_tqpsthe prop segment Tqs/segment
phseg_tqps1the phase segment 1 Tqs/segment
phseg_tqps2the phase segment 2 Tqs/segment
sofif start-of-frame signal is enabled.
wakfilif wakeup filter should be enabled.
samis bus line should be sampled 3 times at sample point (otherwise it is sampled once).
btlmodeif BTLMODE is enabled. true: PS2 length is determined by PHSEG2[2:0] (PHSEG2 values originate from phseg_tqps2). false: PS2 length is the greater of PS1 and IPT (2 Tqs)
Returns
zero if success, otherwise non-zero.

◆ mcp2515_bitrate_simplified()

int mcp2515_bitrate_simplified ( pi_mcp2515_t * pi_mcp2515,
uint16_t baud_rate_kbps )

A simplified bitrate setting function.

Parameters
pi_mcp2515the piMCP2515 handle.
baud_rate_kbpsthe baud rate to use in kbps. Commonly either 500 or 1000.
Returns
zero if success, otherwise non-zero.

◆ mcp2515_cnf_get()

uint8_t mcp2515_cnf_get ( pi_mcp2515_t * pi_mcp2515,
uint8_t cnf )

Get the value of a CNF register.

Parameters
pi_mcp2515the piMCP2515 handle.
cnfThe CNF register index such that cnf being 1 will fetch from CNF1, etc.
Returns
The value of the register.

◆ mcp2515_cnf_set()

int mcp2515_cnf_set ( pi_mcp2515_t * pi_mcp2515,
uint8_t cnf1,
uint8_t cnf2,
uint8_t cnf3 )

Set the values of all CNF register.

Parameters
pi_mcp2515the piMCP2515 handle.
cnf1the value to use for the CNF1 register.
cnf2the value to use for the CNF2 register.
cnf3the value to use for the CNF3 register.
Returns
zero if success, otherwise non-zero.

◆ mcp2515_conf_gpio_devpath()

void mcp2515_conf_gpio_devpath ( pi_mcp2515_t * pi_mcp2515,
char * gpiodev_path )

Override the default path for the gpio device (ex /dev/gpio0).

NOOP unless built for spidev.

This function generally will not need to be used as the library should be able to figure out the device patch. Use this only if it can't in your use case. If it is unable to find the path for you, please open an issue on the project with the details so that can be fixed.

Parameters
pi_mcp2515the piMCP2515 handle.
gpiodev_paththe alternate GPIO device path to use.

◆ mcp2515_conf_spi_devpath()

void mcp2515_conf_spi_devpath ( pi_mcp2515_t * pi_mcp2515,
char * spidev_path )

Override the default path for the spi device (ex /dev/spi0).

NOOP unless built for spidev.

This function generally will not need to be used as the library should be able to figure out the device patch. Use this only if it can't in your use case. If it is unable to find the path for you, please open an issue on the project with the details so that can be fixed.

Parameters
pi_mcp2515the piMCP2515 handle.
spidev_paththe alternate SPI device path to use.

◆ mcp2515_free()

void mcp2515_free ( pi_mcp2515_t * pi_mcp2515)

Cleanup after everything.

Parameters
pi_mcp2515the piMCP2515 handle.

◆ mcp2515_init()

int mcp2515_init ( pi_mcp2515_t ** pi_mcp2515,
uint8_t spi_channel,
uint8_t tx_pin,
uint8_t rx_pin,
uint8_t sck_pin,
uint8_t cs_pin,
uint32_t spi_clock,
uint8_t osc_mhz )

Setup and prepare a pi_mcp2515_t structure based on the parameters provided.

This will still require one of the bitrate setting functions or mcp2515_cnf_set to be used to set up the CNF registers.

Parameters
pi_mcp2515the piMCP2515 handle.
spi_channelthe SPI channel to use which may be 0 or 1.
sck_pinthe SCK pin for SPI communication.
tx_pinthe TX pin for SPI communication.
rx_pinthe RX pin for SPI communication.
spi_clockthe frequency to use for SPI communication in Hz.
osc_mhzthe frequency of the MCP2515 oscillator in MHz.
cs_pinthe GPIO pin to use for SPI chip select.
Returns
zero if success, otherwise non-zero