CAN

Functionality overview

CAN (Controller Area Network) commands provide CAN bus communication through Red Pitaya’s extension connector. Configure CAN interfaces, set bitrate and timing parameters, manage filters, and transmit/receive CAN frames for automotive and industrial control applications.

Important notes

  • FPGA image v0.94 is required to work with CAN.

  • Supports CAN0 and CAN1 interfaces.

  • Proper bitrate and sample point configuration critical for reliable communication.

  • Hardware CAN transceiver required on extension connector.

Code examples

[To be added - examples specific to CAN communication]

Parameters and command table

Parameter options:

  • <n> = {0,1} (set CAN interface CAN0 or CAN1)

  • <bool> = {OFF, ON} Default: OFF

  • <state> = {ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE, BUS_OFF, STOPPED, SLEEPING}

  • <mode> = {LOOPBACK, LISTENONLY, 3_SAMPLES, ONE_SHOT, BERR_REPORTING}

  • <speed> = {1, 10000000} Bitrate speed.

  • <sp> = {0, 0.999} Sample point.

  • <tq> = {unsigned integer} Time quanta in ns.

  • <prop_seg> = {unsigned integer} Propagation segment.

  • <phase_seg1> = {unsigned integer} Phase buffer segment 1.

  • <phase_seg2> = {unsigned integer} Phase buffer segment 2.

  • <sjw> = {unsigned integer} Sinhronisation jump width.

  • <brp> = {unsigned integer} Bitrate per-scaler.

  • <limits> = {<tseg1_min>, <tseg1_max>, <tseg2_min>, <tseg2_max>, <sjw_max>, <brp_min>, <brp_max>, <brp_inc>} Constant:1,16,1,8,4,1,256,1

  • <tseg1_min> = {unsigned integer} Time segment 1 minimum. Constant: 1

  • <tseg1_max> = {unsigned integer} Time segment 1 maximum. Constant: 16

  • <tseg2_min> = {unsigned integer} Time segment 2 minimum. Constant: 1

  • <tseg2_max> = {unsigned integer} Time segment 2 maximum. Constant: 8

  • <sjw_max> = {unsigned integer} Sinhronisation jump width maximum. Constant: 4

  • <brp_min> = {unsigned integer} Bitrate per-scaler minimum. Constant: 1

  • <brp_max> = {unsigned integer} Bitrate per-scaler maximum. Constant: 256

  • <brp_inc> = {unsigned integer} Bitrate per-scaler increment. Constant: 1

  • <clock> = {1...10000000} in Hz. Default: 10000000

  • <tx_err> = {unsigned integer} Tx bus error.

  • <rx_err> = {unsigned integer} Rx bus error.

  • <rs_ms> = {unsigned integer} Restart time in milliseconds. 0 == Restart time OFF. Default: 0

  • <timeout> = {unsigned integer} in milliseconds. Timeout when sending data. Needed if buffer is full 0 == timeout disabled

  • <frame> = {<can_id>, <frame_header>, <is_extended>, <is_error>, <is_rtr>, {<buffer>}} CAN frame composition.

  • <can_id> = {unsigned integer} Destination address on CAN bus

  • <frame_header> = {unsigned integer} CAN frame header

  • <is_extended> = {0,1} IDE bit. Normal (11-bit ID)/Extended (29-bit ID) CAN frame. 0 == Normal/CAN2.0A, 1 == Extended/CAN2.0B.

  • <is_error> = {0,1} Error detected

  • <is_rtr> = {0,1} RTR bit. Mark frame as remote transmission request. 0 == Send data, 1 == Request data.

  • <buffer> = {XXX | XXX,XXX | XXX,XXX,XXX | XXX,...,XXX} Data buffer to transmit (from 0 to 8 Bytes). Data exceeding 8 bytes will be ignored.

  • <filter> = {unsigned integer} CAN ID filter.

  • <mask> = {unsigned integer} Filter mask.

Available Jupyter and API macros:

  • CAN interface - RP_CAN_0, RP_CAN_1

  • CAN state - RP_CAN_STATE_ERROR_ACTIVE, RP_CAN_STATE_ERROR_WARNING, RP_CAN_STATE_ERROR_PASSIVE, RP_CAN_STATE_BUS_OFF, RP_CAN_STATE_STOPPED, RP_CAN_STATE_SLEEPING

  • CAN mode - RP_CAN_MODE_LOOPBACK, RP_CAN_MODE_LISTENONLY, RP_CAN_MODE_3_SAMPLES, RP_CAN_MODE_ONE_SHOT, RP_CAN_MODE_BERR_REPORTING

  • CAN errors:
    • RP_HW_CAN_OK - Success

    • RP_HW_CAN_ESI - Failed start interface

    • RP_HW_CAN_EST - Failed stop interface

    • RP_HW_CAN_ERI - Failed restart interface

    • RP_HW_CAN_EUI - Unknown interface

    • RP_HW_CAN_EBS - Failed set or get bitrate and sample point

    • RP_HW_CAN_EBT - Failed set or get bittiming

    • RP_HW_CAN_EGF - Failed get clock parameters

    • RP_HW_CAN_EGE - Failed get error counters

    • RP_HW_CAN_ERT - Failed set or get restart time

    • RP_HW_CAN_EGS - Failed get current interface state

    • RP_HW_CAN_ECM - Failed get or set controller mode

    • RP_HW_CAN_ECU - Controller mode not supported

    • RP_HW_CAN_ESO - Failed open socket

    • RP_HW_CAN_ESC - Failed close socket

    • RP_HW_CAN_ESA - Failed. Socket already open

    • RP_HW_CAN_ESB - Failed bind socket

    • RP_HW_CAN_ESN - Failed. Socket not opened

    • RP_HW_CAN_ESD - Failed. Missing data

    • RP_HW_CAN_ESBO - Failed. Send buffer overflow

    • RP_HW_CAN_ESTE - Failed. Timeout reached

    • RP_HW_CAN_ESPE - Failed. Poll error

    • RP_HW_CAN_ESE - Failed. Send error

    • RP_HW_CAN_ESFA - Failed add filter. Filter already present in list

    • RP_HW_CAN_ESFS - Failed apply filter

    • RP_HW_CAN_ESEF - Failed to set error handling

    • RP_HW_CAN_ESR - Failed read frame from socket

SCPI

API, Jupyter

DESCRIPTION

ECOSYSTEM

CAN:FPGA <bool>
Example:
CAN:FPGA ON



C++: rp_CanSetFPGAEnable(bool enable)

Python: rp_CanSetFPGAEnable(enable)



Enables FPGA forwarding from CAN controller to GPIO.

2.00-30 and up

CAN:FPGA? > <bool>
Example:
CAN:FPGA? > ON



C++: rp_CanGetFPGAEnable(bool *state)

Python: rp_CanGetFPGAEnable()



Gets the status of FPGA forwarding from CAN controller to GPIO.

2.00-30 and up

CAN<n>:START
Example:
CAN0:START



C++: rp_CanStart(rp_can_interface_t interface)

Python: rp_CanStart(<interface>)



Sets the state of the specified interface to UP.
Bitrate must be set before starting or restarting the interface.




2.00-30 and up

CAN<n>:STOP
Example:
CAN0:STOP



C++: rp_CanStop(rp_can_interface_t interface)

Python: rp_CanStop(<interface>)



Sets the state of the specified interface to DOWN.

2.00-30 and up

CAN<n>:RESTART
Example:
CAN0:RESTART



C++: rp_CanRestart(rp_can_interface_t interface)

Python: rp_CanRestart(<interface>)



Restarts the specified interface.
Bitrate must be set before starting or restarting the interface.




2.00-30 and up

CAN<n>:STATE? > <state>
Example:
CAN0:STATE? > ERROR_ACTIVE




C++: rp_CanGetState(rp_can_interface_t interface, rp_can_state_t *state)

Python: rp_CanGetState(<interface>)




Returns the current state of the CAN interface.
ERROR_ACTIVE - RX/TX error count < 96
ERROR_WARNING - RX/TX error count < 128
ERROR_PASSIVE - RX/TX error count < 256
BUS_OFF - RX/TX error count >= 256
STOPPED - Device is stopped
SLEEPING - Device is sleeping

2.00-30 and up

CAN<n>:BITRate <speed>
Example:
CAN0:BITRate 200000



C++: rp_CanSetBitrate(rp_can_interface_t interface, uint32_t bitRate)

Python: rp_CanGetStateName(<state>)



Sets the bitrate (Hz) for the specified interface. Sample point is
set automatically.




2.00-30 and up

CAN<n>:BITRate:SP <speed>,<sp>
Example:
CAN0:BITRate:SP 200000,0.8



C++: rp_CanSetBitrateAndSamplePoint(rp_can_interface_t interface, uint32_t bitRate, float samplePoint)

Python: rp_CanSetBitrateAndSamplePoint(<interface>, <bitRate>, <samplePoint>)



Sets the bitrate (Hz) and sample point (0.1%) for the specified interface.

2.00-30 and up

CAN<n>:BITRate:SP? > <speed>,<sp>
Example:
CAN0:BITRate:SP? > 200000,0.8




C++: rp_CanGetBitrateAndSamplePoint(rp_can_interface_t interface, uint32_t *bitRate, float *samplePoint)

Python: rp_CanGetBitrateAndSamplePoint(<interface>)




Shows the real bit-rate speed``in bits/sec and the sample-point ``sp
in the range 0.000…0.999.
If the calculation of bit-timing parameters is enabled in the kernel
(CONFIG_CAN_CALC_BITTIMING=y), the bit-timing can be defined by setting
the “bitrate” argument. Optionally, the “sample-point” can be specified.
By default it’s 0.000 assuming CIA-recommended sample-points.

2.00-30 and up

CAN<n>:BITTiming <tq>,<prop_seg>,<phase_seg1>,<phase_seg2>,<sjw>,<brp>
Example:
CAN0:BITTiming 1000,1,2,1,1,10



C++: rp_CanSetBitTiming(rp_can_interface_t interface, rp_can_bittiming_t bitTiming)

Python: rp_CanSetBitTiming(<interface>, <bitTiming>)



Set bit-timing settings.




2.00-30 and up

CAN<n>:BITTiming? > <tq>,<prop_seg>,<phase_seg1>,<phase_seg2>,<sjw>,<brp>
Example:
CAN0:BITTiming? > 1000,1,2,1,1,10



C++: rp_CanGetBitTiming(rp_can_interface_t interface, rp_can_bittiming_t *bitTiming)

Python: rp_CanGetBitTiming(<interface>, <bitTiming>)



Shows the time quanta tq in ns, propagation segment prop_seg,
phase buffer segment 1 and 2 phase_seg1, phase_seg2, and the
synchronisation jump width sjw in units of time quanta.
These settings define CAN bit timing in a hardware-independent format
proposed by Bosch CAN 2.0 specification (Chapter 8).

2.00-30 and up

CAN<n>:BITTiming:Limits? > <limits>
Example:
CAN0:BITTiming:Limits? > 1,16,1,8,4,1,256,1




C++: rp_CanGetBitTimingLimits(rp_can_interface_t interface, rp_can_bittiming_limits_t *bitTiming)

Python: rp_CanGetBitTimingLimits(<interface>, <bitTiming>)




Displays the CAN controller bit timing constants (“sja1000”), the
minimum and maximum values of time segment 1 and 2, the synchronisation
jump width swj in time quanta (tq) units, the bit rate
prescaler brp and the CAN system clock frequency in Hz.
These constants can be used for user defined (non-standard) bit timing
calculation algorithms in user space.

2.00-30 and up

CAN<n>:CLOCK? > <clock>
Example:
CAN0:CLOCK? > 10000000



C++: rp_CanGetClockFreq(rp_can_interface_t interface, uint32_t *freq)

Python: rp_CanGetClockFreq(<interface>)



Returns the CAN clock frequency in Hz.

2.00-30 and up

CAN<n>:BUS:ERROR? > <tx_err>,<rx_err>
Example:
CAN0:BUS:ERROR? > 0,0



C++: rp_CanGetBusErrorCounters(rp_can_interface_t interface, uint16_t *tx, uint16_t *rx)

Python: rp_CanGetBusErrorCounters(<interface>)



Returns the number of errors on the RX and TX busses.

2.00-30 and up

CAN<n>:Restart:Time <rs_ms>
Example:
CAN0:Restart:Time 10



C++: rp_CanSetRestartTime(rp_can_interface_t interface, uint32_t ms)

Python: rp_CanSetRestartTime(<interface>, <ms>)



Automatic restart delay time. If set to a non-zero value, the CAN
controller will automatically restart after the specified delay time
in milliseconds in the event of a bus-off condition.
Set to 0 (OFF) by default.


2.00-30 and up

CAN<n>:Restart:Time? > <rs_ms>
Example:
CAN0:Restart:Time? > 10



C++: rp_CanGetRestartTime(rp_can_interface_t interface, uint32_t *ms)

Python: rp_CanGetRestartTime(<interface>)



Returns the current restart delay time (ms).

2.00-30 and up

CAN<n>:MODE <mode>,<bool>
Example:
CAN0:MODE LOOPBACK,ON




C++: rp_CanSetControllerMode(rp_can_interface_t interface, rp_can_mode_t mode, bool state)

Python: rp_CanSetControllerMode(<interface>, <mode>, <state>)




Sets the controller mode:
LOOPBACK - Internal connection between TX and RX
LISTENONLY - Disabled TX, RX only, CAN bus monitoring
3_SAMPLES - Tripple sampling mode
ONE_SHOT - One shot mode
BERR_REPORTING - Buss error reporting
Multiple modes can be selected at the same time.

2.00-30 and up

CAN<n>:MODE? <mode> > <bool>
Example:
CAN0:MODE? LOOPBACK > ON



C++: rp_CanGetControllerMode(rp_can_interface_t interface, rp_can_mode_t mode, bool *state)

Python: rp_CanGetControllerMode(<interface>, <mode>)



Checks the status of the selected mode.

2.00-30 and up

CAN<n>:OPEN
Example:
CAN0:OPEN



C++: rp_CanOpen(rp_can_interface_t interface)

Python: rp_CanOpen(<interface>)



Opens a socket connection for the specified interface.

2.00-30 and up

CAN<n>:CLOSE
Example:
CAN0:CLOSE



C++: rp_CanClose(rp_can_interface_t interface)

Python: rp_CanClose(<interface>)



Closes an open connection.

2.00-30 and up

CAN<n>:Send<can_id> <buffer>
Example:
CAN0:Send123 1,2,3



C++: rp_CanSend(rp_can_interface_t interface, uint32_t canId, unsigned char *data, uint8_t dataSize,
bool isExtended, bool rtr, uint32_t timeout)
Python: rp_CanSend(<interface>, <canId>, <data>, <dataSize>, <isExtended>, <rtr>, <timeout>)



Sends the frame to the specified address can_id.
Data exceeding 8 bytes will be ignored.

C++, Python: Option to enable RTR, Extended Frame and add timeout
(see below).

2.00-30 and up

CAN<n>:Send<can_id>:RTR <buffer>
Example:
Example:
CAN0:Send123 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)




Sends the frame to the specified address can_id marked as
the “Remote Transmission Request” (rtr == true).





2.00-30 and up

CAN<n>:Send<can_id>:Timeout<timeout> <buffer>
Example:
CAN0:Send123:Timeout2000 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)



Sends the frame to the specified address can_id with a timeout
for sending if the send buffer is full.




2.00-30 and up

CAN<n>:Send<can_id>:Ext
Example:
CAN0:Send123:Ext 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)



Sends the frame to the specified address can_id using the extended
packet frame (isExtended == True).




2.00-30 and up

CAN<n>:Send<can_id>:Timeout<timeout>:Ext <buffer>
Example:
CAN0:Send123:Timeout2000:Ext 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)



Sends the frame to the specified address can_id using the extended
packet frame (isExtended == True) with a timeout for sending if
send buffer is full.



2.00-30 and up

CAN<n>:Send<can_id>:Timeout<timeout>:RTR <buffer>
Example:
CAN0:Send123:Timeout2000:RTR 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)



Sends the frame to the specified address can_id marked as the
“Remote Transmission Request” (rtr == true).
There is a timeout for sending if the send buffer is full.



2.00-30 and up

CAN<n>:Send<can_id>:Ext:RTR
Example:
CAN0:Send123:Ext:RTR 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)



Sends the frame to the specified address can_id using the extended
packet frame (isExtended == True) and marked as the
the “Remote Transmission Request” (rtr == true).



2.00-30 and up

CAN<n>:Send<can_id>:Timeout<timeout>:Ext:RTR <buffer>
Example:
CAN0:Send123:Timeout2000:Ext:RTR 1,2,3



C++: ‘’ (See above)

Python: ‘’ (See above)



Sends the frame to the specified address can_id using the extended
packet frame (isExtended == True) and marked as the
the “Remote Transmission Request” (rtr == true).
There is a timeout for sending if the send buffer is full.


2.00-30 and up

CAN<n>:Read? > <frame>
Example:
CAN0:Read? > 123,123,0,0,0,3,{1,2,3}



C++: rp_CanRead(rp_can_interface_t interface, uint32_t timeout, rp_can_frame_t *frame)

Python: rp_CanRead(<interface>, <timeout>, <frame>)



Reads 1 frame from the specified CAN interface.


C++, Python: Option to add timeout (see below).


2.00-30 and up

CAN<n>:Read:Timeout<timeout>? > <frame>
Example:
CAN0:Read:Timeout2000? > 123,123,0,0,0,3,{1,2,3}



C++: ‘’ (See above)

Python: ‘’ (See above)



Reads 1 frame from the specified CAN interface. If no frame is received
within the timeout time, an empty frame is returned.




2.00-30 and up

CAN<n>:Filter:Add <filter>,<mask>
Example:
CAN0:Filter:Add 0,0



C++: rp_CanAddFilter(rp_can_interface_t interface, uint32_t filter, uint32_t mask)

Python: rp_CanAddFilter(<interface>, <filter>, <mask>)



Adds the specified “ID filter” filter (with mask mask) to the
list of filters. Once all the filters have been added, the command to
apply filters to the socket must be called CAN<n>:Filter:Set.
A filter matches if <received_can_id> & mask == filter & mask


2.00-30 and up

CAN<n>:Filter:Remove <filter>,<mask>
Example:
CAN0:Filter:Remove 0,0



C++: rp_CanRemoveFilter(rp_can_interface_t interface, uint32_t filter, uint32_t mask)

Python: rp_CanRemoveFilter(<interface>, <filter>, <mask>)



Deletes the specified “ID filter” from the filter list of the specified
CAN interface.




2.00-30 and up

CAN<n>:Filter:Clear
Example:
CAN0:Filter:Clear



C++: rp_CanClearFilter(rp_can_interface_t interface)

Python: rp_CanClearFilter(<interface>)



Removes all filters from the filter list of the specified CAN interface.

2.00-30 and up

CAN<n>:Filter:Set
Example:
CAN0:Filter:Set



C++: rp_CanSetFilter(rp_can_interface_t interface, bool isJoinFilter)

Python: rp_CanSetFilter(<interface>, <isJoinFilter>)



Applies the list of ID filters to the socket connection of the specified
CAN interface.




2.00-30 and up

CAN<n>:SHOW:ERROR
Example:
CAN0:SHOW:ERROR



C++: rp_CanShowErrorFrames(rp_can_interface_t interface, bool enable)

Python: rp_CanShowErrorFrames(<interface>, <enable>)



When this mode is on, all errors will be converted to data frames with
the error frame marker.




2.00-30 and up