Working fork to test F0 application
Fork of CANnucleo by
CAN Class Reference
A can bus client, used for communicating with can devices. More...
#include <CAN.h>
Public Member Functions | |
CAN (PinName rd, PinName td, FunctionalState abom=ENABLE) | |
Creates an CAN interface connected to specific pins. | |
virtual | ~CAN () |
int | frequency (int hz) |
Set the frequency of the CAN interface. | |
int | write (CANMessage msg) |
Write a CANMessage to the bus. | |
int | read (CANMessage &msg, int handle=0) |
Read a CANMessage from the bus. | |
void | reset () |
Reset CAN interface. | |
void | monitor (bool silent) |
Puts or removes the CAN interface into silent monitoring mode. | |
int | mode (Mode mode) |
Change CAN operation to the specified mode. | |
int | filter (unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0) |
Filter out incomming messages. | |
unsigned char | rderror () |
Returns number of read errors to detect read overflow errors. | |
unsigned char | tderror () |
Returns number of write errors to detect write overflow errors. | |
void | attach (void(*fptr)(void), IrqType type=RxIrq) |
Attach a function to call whenever a CAN frame received interrupt is generated. | |
template<typename T > | |
void | attach (T *tptr, void(T::*mptr)(void), IrqType type=RxIrq) |
Attach a member function to call whenever a CAN frame received interrupt is generated. | |
Static Public Member Functions | |
static void | _irq_handler (uint32_t id, CanIrqType type) |
Detailed Description
A can bus client, used for communicating with can devices.
Definition at line 131 of file CAN.h.
Constructor & Destructor Documentation
CAN | ( | PinName | rxPin, |
PinName | txPin, | ||
FunctionalState | abom = ENABLE |
||
) |
Creates an CAN interface connected to specific pins.
Constructor.
- Parameters:
-
rd read from transmitter td transmit to transmitter
Example:
#include "mbed.h" #include "CAN.h" Ticker ticker; DigitalOut led1(LED1); CAN can(PA_11, PA_12); char counter = 0; void send() { if(can.write(CANMessage(1337, &counter, 1))) { printf("Message sent: %d\n", counter); counter++; } led1 = !led1; } int main() { ticker.attach(&send, 1); CANMessage msg; while(1) { if(can.read(msg)) { printf("Message received: %d\n\n", msg.data[0]); led1 = !led1; } wait(0.2); } }
Constructor
- Parameters:
-
rd CAN receiver pin name td CAN transmitter pin name abom Automatic recovery from bus-off state (default value set to enabled)
- Note:
- Constructs an instance of CAN class
- Parameters:
-
rxPin,: CAN Rx pin name txPin,: CAN Tx pin name abom,: Automatic recovery from bus-off state (defaults to enabled)
- Return values:
-
Member Function Documentation
void _irq_handler | ( | uint32_t | id, |
CanIrqType | type | ||
) | [static] |
void attach | ( | T * | tptr, |
void(T::*)(void) | mptr, | ||
IrqType | type = RxIrq |
||
) |
Attach a member function to call whenever a CAN frame received interrupt is generated.
- Parameters:
-
tptr pointer to the object to call the member function on mptr pointer to the member function to be called event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
void attach | ( | void(*)(void) | fptr, |
IrqType | type = RxIrq |
||
) |
Attach a function to call whenever a CAN frame received interrupt is generated.
Attaches handler funcion to CAN1 RX0 Interrupt.
- Parameters:
-
fptr A pointer to a void function, or 0 to set as none event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
- Note:
- Only CAN1 RX0 Interrupt supported
- Parameters:
-
fptr,: pointer to a void (*)(void) function type,: not used (only CAN1 RX0 Interrupt supported)
- Return values:
-
int filter | ( | unsigned int | id, |
unsigned int | mask, | ||
CANFormat | format = CANAny , |
||
int | handle = 0 |
||
) |
Filter out incomming messages.
Sets up a CAN filter.
- Parameters:
-
id the id to filter on mask the mask applied to the id format format to filter on (Default CANAny) handle message filter handle (Optional)
- Returns:
- 0 - successful 1 - error 2 - busy 3 - time out
- Note:
- At the present, CANnucleo supports only mask mode and 32-bit filter scale. Identifier list mode filtering and 16-bit filter scale are not supported. There are 14 filters available (0 - 13) for the application to set up. Each filter is a 32-bit filter defined by a filter ID and a filter mask. If no filter is set up then no CAN message is accepted (received)! That's why filter #0 is set up in the constructor to receive all CAN messages by default. On reception of a message it is compared with filter #0. If there is a match, the message is stored. If there is no match, the incoming identifier is then compared with the next filter. If the received identifier does not match any of the identifiers configured in the filters, the message is discarded by hardware without disturbing the software.
- Parameters:
-
id,: 'Filter ID' defines the bit values to be compared with the corresponding received bits
Mapping of 32-bits (4-bytes) : | STID[10:3] | STID[2:0] EXID[17:13] | EXID[12:5] | EXID[4:0] IDE RTR 0 |
STID - Stardard Identifier bits EXID - Extended Identifier bits [x:y]- bit range IDE - Identifier Extension bit (0 -> Standard Identifier, 1 -> Extended Identifier) RTR - Remote Transmission Request bit (0 -> Remote Transmission Request, 1 -> Standard message)
- Parameters:
-
mask,: 'Filter mask' defines which bits of the 'Filter ID' are compared with the received bits and which bits are disregarded.
Mapping of 32-bits (4-bytes) : | STID[10:3] | STID[2:0] EXID[17:13] | EXID[12:5] | EXID[4:0] IDE RTR 0 |
STID - Stardard Identifier bits EXID - Extended Identifier bits [x:y]- bit range IDE - Identifier Extension bit RTR - Remote Transmission Request bit
1 -> bit is considered 0 -> bit is disregarded
---------------------------------------- Example of filter set up and filtering: ----------------------------------------
Let's assume we would like to receive only messages with standard identifier STID = 0x0207 (STID[15:0] = 00000010 00000111)
We map the STID to filter ID by shifting the bits appropriately: Filter id = STID << (16 + (15 - 10)) = STID << 21 = 01000000 11100000 00000000 00000000 = 0x40E00000
To compare only the bits representing STID we set the filter mask adequately: Filter mask = 11111111 11100000 00000000 00000100 = 0xFFE00004 |||||||| ||| | -------- --- | | | | STID[10:3] STID[2:0] IDE
Keep in mind that filter #0 was already set up in the constructor to receive all CAN messages by default. So we have to reconfigure it. If we were set up filter #1 here then filter #0 would receive all the messages and no message would reach filter #1!
To set up filter #0 we call: can.filter(0x0207 << 21, 0xFFE00004, CANAny, 0);
Only these bits (set to 1) of filter id are compared with the corresponding bits of received message (the others are disregarded) | --------------------------------- |||||||| ||| | Filter mask = 11111111 11100000 00000000 00000100 (= 0xFFE00004) Filter id = 01000000 11100000 00000000 00000000 (= 0x40E00000) |||||||| ||| | --------------------------------- | To receive the message the values of these bits must match. Otherwise the message is passed to the next filter or discarded if this was the last active filter. | --------------------------------- |||||||| ||| | Received id = 01000000 11100000 00000000 00000010 (= 0x40E00002) ||||| |||||||| ||||| || ----------------------- | These bits (set to 0 in filter mask) are disregarded (masked). They can have arbitrary values.
NOTE: For the meaning of individual bits see the mapping of 32-bits explained above.
- Parameters:
-
format,: This parameter must be CANAny handle,: Selects the filter. This parameter must be a number between 0 and 13. retval,: 0 - successful 1 - error 2 - busy 3 - time out
int frequency | ( | int | f ) |
int mode | ( | Mode | mode ) |
Change CAN operation to the specified mode.
- Parameters:
-
mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
- Returns:
- 0 if mode change failed or unsupported, 1 if mode change was successful
- Note:
- Parameters:
-
@retval
void monitor | ( | bool | silent ) |
unsigned char rderror | ( | void | ) |
int read | ( | CANMessage & | msg, |
int | handle = 0 |
||
) |
Read a CANMessage from the bus.
- Parameters:
-
msg A CANMessage to read to. handle message filter handle (0 for any message)
- Returns:
- 0 if no message arrived, 1 if message arrived
- Note:
- Parameters:
-
@retval
void reset | ( | void | ) |
unsigned char tderror | ( | void | ) |
int write | ( | CANMessage | msg ) |
Write a CANMessage to the bus.
- Parameters:
-
msg The CANMessage to write.
- Returns:
- 0 if write failed, 1 if write was successful
- Note:
- Parameters:
-
@retval
Generated on Tue Jul 12 2022 20:03:31 by 1.7.2