Controller Area Network library for NUCLEO boards equipped with CAN peripheral.
Dependents: Nucleo-Courtois CANBLE CANnucleo_Hello3 Nucleo_Serialprintf ... more
CAN Class Reference
A can bus client, used for communicating with can devices. More...
#include <CANnucleo.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 (mbed::Callback< void()> func, IrqType type=RxIrq) |
Attach a function to call whenever a CAN frame received interrupt is generated. | |
template<typename T > | |
void | attach (T *obj, void(T::*method)(), IrqType type=RxIrq) |
Attach a member function to call whenever a CAN frame received interrupt is generated. | |
template<typename T > | |
void | attach (T *obj, void(*method)(T *), 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 124 of file CANnucleo.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:
-
Definition at line 30 of file CANnucleo.cpp.
~CAN | ( | void | ) | [virtual] |
Member Function Documentation
void _irq_handler | ( | uint32_t | id, |
CanIrqType | type | ||
) | [static] |
void attach | ( | T * | obj, |
void(T::*)() | method, | ||
IrqType | type = RxIrq |
||
) |
Attach a member function to call whenever a CAN frame received interrupt is generated.
- Parameters:
-
obj pointer to the object to call the member function on method pointer to the member function to be called event Which CAN interrupt to attach the member function to (only CAN::RxIrq for message received is supported)
Definition at line 289 of file CANnucleo.h.
void attach | ( | mbed::Callback< void()> | func, |
IrqType | type = RxIrq |
||
) |
Attach a function to call whenever a CAN frame received interrupt is generated.
Attaches handler funcion to CAN1 RX0 Interrupt.
- Parameters:
-
func A pointer to a void function, or 0 to set as none event Which CAN interrupt to attach the member function to (only CAN::RxIrq for message received is supported)
- Note:
- Only CAN1 RX0 Interrupt supported
- Parameters:
-
fptr,: pointer to a void (*)(void) function type,: not used (only CAN1 RX0 Interrupt supported)
- Return values:
-
Definition at line 256 of file CANnucleo.cpp.
void attach | ( | T * | obj, |
void(*)(T *) | method, | ||
IrqType | type = RxIrq |
||
) |
Attach a member function to call whenever a CAN frame received interrupt is generated.
- Parameters:
-
obj pointer to the object to call the member function on method pointer to the member function to be called event Which CAN interrupt to attach the member function to (only CAN::RxIrq for message received is supported)
Definition at line 302 of file CANnucleo.h.
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)
- Return values:
-
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
Recall that filter #0 has been 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 reconfigure (set up) filter #0 we call: can.filter(0x0207 << 21, 0xFFE00004, CANAny, 0);
Only these bits of 'Filter id' (set to 1 here in 'Filter mask') 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.
- Return values:
-
0 - successful 1 - error 2 - busy 3 - time out
Definition at line 242 of file CANnucleo.cpp.
int frequency | ( | int | f ) |
Set the frequency of the CAN interface.
- Parameters:
-
hz The bus frequency in hertz
- Returns:
- 1 if successful, 0 otherwise
- Note:
- Parameters:
-
@retval
Definition at line 53 of file CANnucleo.cpp.
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
Definition at line 142 of file CANnucleo.cpp.
void monitor | ( | bool | silent ) |
Puts or removes the CAN interface into silent monitoring mode.
- Parameters:
-
silent boolean indicating whether to go into silent mode or not
- Note:
- Parameters:
-
@retval
Definition at line 130 of file CANnucleo.cpp.
unsigned char rderror | ( | void | ) |
Returns number of read errors to detect read overflow errors.
- Note:
- Parameters:
-
@retval
Definition at line 104 of file CANnucleo.cpp.
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
Definition at line 79 of file CANnucleo.cpp.
void reset | ( | void | ) |
Reset CAN interface.
To use after error overflow.
- Note:
- Parameters:
-
@retval
Definition at line 92 of file CANnucleo.cpp.
unsigned char tderror | ( | void | ) |
Returns number of write errors to detect write overflow errors.
- Note:
- Parameters:
-
@retval
Definition at line 117 of file CANnucleo.cpp.
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
Definition at line 66 of file CANnucleo.cpp.
Generated on Wed Jul 13 2022 09:11:29 by 1.7.2