hsk-libs-dev
270
High Speed Karlsruhe XC878 library collection
|
HSK Controller Area Network headers. More...
Go to the source code of this file.
Macros | |
#define | CAN_ERROR 0xff |
Value returned by functions in case of an error. More... | |
#define | CAN0 0 |
CAN node 0. More... | |
#define | CAN1 1 |
CAN node 1. More... | |
#define | CAN0_IO_P10_P11 0 |
CAN node 0 IO RX on P1.0, TX on P1.1. More... | |
#define | CAN0_IO_P16_P17 1 |
CAN node 0 IO RX on P1.6, TX on P1.7. More... | |
#define | CAN0_IO_P34_P35 2 |
CAN node 0 IO RX on P3.4, TX on P3.5. More... | |
#define | CAN0_IO_P40_P41 3 |
CAN node 0 IO RX on P4.0, TX on P4.1. More... | |
#define | CAN1_IO_P01_P02 4 |
CAN node 1 IO RX on P0.1, TX on P0.2. More... | |
#define | CAN1_IO_P14_P13 5 |
CAN node 1 IO RX on P1.4, TX on P1.3. More... | |
#define | CAN1_IO_P32_P33 6 |
CAN node 1 IO RX on P3.2, TX on P3.3. More... | |
#define | CAN_ENDIAN_INTEL 0 |
Little endian signal encoding. More... | |
#define | CAN_ENDIAN_MOTOROLA 1 |
Big endian signal encoding. More... | |
#define | CAN_STATUS_LEC 0 |
The Last Error Code field provides the error triggered by the last message on the bus. More... | |
#define | CAN_STATUS_TXOK 1 |
Message Transmitted Successfully. More... | |
#define | CAN_STATUS_RXOK 2 |
Message Received Successfully. More... | |
#define | CAN_STATUS_ALERT 3 |
Alert Warning. More... | |
#define | CAN_STATUS_EWRN 4 |
Error Warning Status. More... | |
#define | CAN_STATUS_BOFF 5 |
Bus-off Status. More... | |
Typedefs | |
typedef ubyte | hsk_can_node |
CAN node identifiers. More... | |
typedef ubyte | hsk_can_msg |
CAN message object identifiers. More... | |
typedef ubyte | hsk_can_fifo |
CAN message FIFO identifiers. More... | |
Functions | |
void | hsk_can_init (const ubyte pins, const ulong baud) |
Setup CAN communication with the desired baud rate. More... | |
void | hsk_can_enable (const hsk_can_node node) |
Go live on the CAN bus. More... | |
void | hsk_can_disable (const hsk_can_node node) |
Disable a CAN node. More... | |
ubyte | hsk_can_status (const hsk_can_node node, const ubyte field) |
Returns a status field of a CAN node. More... | |
hsk_can_msg | hsk_can_msg_create (const ulong id, const bool extended, const ubyte dlc) |
Creates a new CAN message. More... | |
ubyte | hsk_can_msg_connect (const hsk_can_msg msg, const hsk_can_node node) |
Connect a message object to a CAN node. More... | |
ubyte | hsk_can_msg_disconnect (const hsk_can_msg msg) |
Disconnect a CAN message object from its CAN node. More... | |
ubyte | hsk_can_msg_delete (const hsk_can_msg msg) |
Delete a CAN message object. More... | |
void | hsk_can_msg_getData (const hsk_can_msg msg, ubyte *const msgdata) |
Gets the current data in the CAN message. More... | |
void | hsk_can_msg_setData (const hsk_can_msg msg, const ubyte *const msgdata) |
Sets the current data in the CAN message. More... | |
void | hsk_can_msg_send (const hsk_can_msg msg) |
Request transmission of a message. More... | |
bool | hsk_can_msg_sent (const hsk_can_msg msg) |
Return whether the message was successfully sent between this and the previous call of this method. More... | |
void | hsk_can_msg_receive (const hsk_can_msg msg) |
Return the message into RX mode after sending a message. More... | |
bool | hsk_can_msg_updated (const hsk_can_msg msg) |
Return whether the message was updated via CAN bus between this call and the previous call of this method. More... | |
hsk_can_fifo | hsk_can_fifo_create (ubyte size) |
Creates a message FIFO. More... | |
void | hsk_can_fifo_setupRx (hsk_can_fifo fifo, const ulong id, const bool extended, const ubyte dlc) |
Set the FIFO up for receiving messages. More... | |
void | hsk_can_fifo_setRxMask (const hsk_can_fifo fifo, ulong msk) |
Changes the ID matching mask of an RX FIFO. More... | |
ubyte | hsk_can_fifo_connect (const hsk_can_fifo fifo, const hsk_can_node node) |
Connect a FIFO to a CAN node. More... | |
ubyte | hsk_can_fifo_disconnect (const hsk_can_fifo fifo) |
Disconnect a FIFO from its CAN node. More... | |
ubyte | hsk_can_fifo_delete (const hsk_can_fifo fifo) |
Delete a FIFO. More... | |
void | hsk_can_fifo_next (const hsk_can_fifo fifo) |
Select the next FIFO entry. More... | |
ulong | hsk_can_fifo_getId (const hsk_can_fifo fifo) |
Returns the CAN ID of the selected FIFO entry. More... | |
bool | hsk_can_fifo_updated (const hsk_can_fifo fifo) |
Return whether the currently selected FIFO entry was updated via CAN bus between this call and the previous call of this method. More... | |
void | hsk_can_fifo_getData (const hsk_can_fifo fifo, ubyte *const msgdata) |
Gets the data from the currently selected FIFO entry. More... | |
void | hsk_can_data_setSignal (ubyte *const msg, const bool motorola, const bool sign, const ubyte bitPos, const char bitCount, const ulong value) |
Sets a signal value in a data field. More... | |
ulong | hsk_can_data_getSignal (const ubyte *const msg, const bool motorola, const bool sign, const ubyte bitPos, const char bitCount) |
Get a signal value from a data field. More... | |
HSK Controller Area Network headers.
This file contains the function prototypes to initialize and engage in CAN communication over the builtin CAN nodes 0 and 1.
The recommended way to use messages and signals is not to specify them inline, but to provide defines with a set of parameters.
These tupples should follow the following pattern:
The symbols have the following meaning:
Tuples using the specified format can directly be used as parameters for several functions in the library.
There are 7 port pairs available for CAN communication, check the CANn_IO_* defines. Four for the node CAN0 and three for CAN1.
The MultiCAN module offers up to 32 message objects. New messages are set up for receiving messages. Message object can be switched from RX to TX mode and back with the hsk_can_msg_send() and hsk_can_msg_receive() functions.
FIFOs are the weapon of choice when dealing with large numbers of individual messages or when receving multiplexed data. In most use cases only the latest version of a message is relevant and FIFOs are not required. But messages containing multiplexed signals may contain critical signals that would be overwritten by a message with the same ID, but a different multiplexor.
If more message IDs than available message objects are used to send and/or receive data, there is no choice but to use a FIFO.
Currently only RX FIFOs are supported.
A FIFO can act as a buffer the CAN module can store message data in until it can be dealt with. The following example illustrates how to read from a FIFO:
When using a mask to accept several messages checking the ID becomes necessary:
FIFOs draw from the same message object pool regular message objects do.
The hsk_can_data_setSignal() and hsk_can_data_getSignal() functions allow writing and reading signals across byte boundaries to and from a buffer.
For big endian signals the bit position of the most significant bit must be supplied (highest bit in the first byte). For little endian signals the least significant bit must be supplied (lowest bit in the first byte).
This conforms to the way signal positions are stored in Vector CANdb++ DBC files.
#define CAN0 0 |
CAN node 0.
#define CAN0_IO_P10_P11 0 |
CAN node 0 IO RX on P1.0, TX on P1.1.
#define CAN0_IO_P16_P17 1 |
CAN node 0 IO RX on P1.6, TX on P1.7.
#define CAN0_IO_P34_P35 2 |
CAN node 0 IO RX on P3.4, TX on P3.5.
#define CAN0_IO_P40_P41 3 |
CAN node 0 IO RX on P4.0, TX on P4.1.
#define CAN1 1 |
CAN node 1.
#define CAN1_IO_P01_P02 4 |
CAN node 1 IO RX on P0.1, TX on P0.2.
#define CAN1_IO_P14_P13 5 |
CAN node 1 IO RX on P1.4, TX on P1.3.
#define CAN1_IO_P32_P33 6 |
CAN node 1 IO RX on P3.2, TX on P3.3.
#define CAN_ENDIAN_INTEL 0 |
Little endian signal encoding.
#define CAN_ENDIAN_MOTOROLA 1 |
Big endian signal encoding.
#define CAN_ERROR 0xff |
Value returned by functions in case of an error.
typedef ubyte hsk_can_fifo |
CAN message FIFO identifiers.
typedef ubyte hsk_can_msg |
CAN message object identifiers.
typedef ubyte hsk_can_node |
CAN node identifiers.
ulong hsk_can_data_getSignal | ( | const ubyte *const | msg, |
const bool | motorola, | ||
const bool | sign, | ||
const ubyte | bitPos, | ||
const char | bitCount | ||
) |
Get a signal value from a data field.
msg | The message data field to read from |
motorola | Indicates big endian (Motorola) encoding |
sign | Indicates whether the value has a signed type |
bitPos | The bit position of the signal |
bitCount | The length of the signal |
void hsk_can_data_setSignal | ( | ubyte *const | msg, |
const bool | motorola, | ||
const bool | sign, | ||
const ubyte | bitPos, | ||
const char | bitCount, | ||
const ulong | value | ||
) |
Sets a signal value in a data field.
msg | The message data field to write into |
motorola | Indicates big endian (Motorola) encoding |
sign | Indicates whether the value has a signed type |
bitPos | The bit position of the signal |
bitCount | The length of the signal |
value | The signal value to write into the data field |
The sign parameter is not required for setting signals, it is just there so that one signal configuration tuple suffices for hsk_can_data_setSignal() and hsk_can_data_getSignal().
void hsk_can_disable | ( | const hsk_can_node | node | ) |
Disable a CAN node.
This completely shuts down a CAN node, cutting it off from the internal clock, to reduce energy consumption.
node | The CAN node to disable |
void hsk_can_enable | ( | const hsk_can_node | node | ) |
Go live on the CAN bus.
To be called when everything is set up.
node | The CAN node to enable |
ubyte hsk_can_fifo_connect | ( | const hsk_can_fifo | fifo, |
const hsk_can_node | node | ||
) |
Connect a FIFO to a CAN node.
fifo | The identifier of the FIFO |
node | The CAN node to connect to |
CAN_ERROR | The given FIFO is not valid |
0 | Success |
hsk_can_fifo hsk_can_fifo_create | ( | ubyte | size | ) |
Creates a message FIFO.
FIFOs can be used to ensure that multiplexed signals are not lost.
For receiving multiplexed signals it is recommended to use a FIFO as large as the number of multiplexed messages that might occur in a single burst.
If the multiplexor is large, e.g. 8 bits, it's obviously not possible to carve a 256 messages FIFO out of 32 message objects. Make an educated guess and hope that the signal provider is not hostile.
If the number of available message objects is at least one, but less than the requested length this function succeeds, but the FIFO is only created as long as possible.
size | The desired FIFO size |
CAN_ERROR | Creating the FIFO failed |
[0;32[ | The created FIFO id |
Slave Objects
Slave objects are put into the same list as the base message object, so it can be used as a slave as well.
Always configure slave messages as TXSLAVEs, because in RXMODE the setting is ignored anyway.
Message Pointers
MOFGPRn of the base object holds the message pointers that define the list boundaries. SEL will be used to keep track of where to read/write the next message when interacting with the FIFO.
ubyte hsk_can_fifo_delete | ( | const hsk_can_fifo | fifo | ) |
Delete a FIFO.
fifo | The identifier of the FIFO |
CAN_ERROR | The given FIFO is not valid |
0 | Success |
ubyte hsk_can_fifo_disconnect | ( | const hsk_can_fifo | fifo | ) |
Disconnect a FIFO from its CAN node.
This takes the FIFO out of active communication, without deleting it.
fifo | The identifier of the FIFO |
CAN_ERROR | The given FIFO is not valid |
0 | Success |
void hsk_can_fifo_getData | ( | const hsk_can_fifo | fifo, |
ubyte *const | msgdata | ||
) |
Gets the data from the currently selected FIFO entry.
This writes DLC bytes from the FIFO entry into msgdata.
fifo | The identifier of the FIFO |
msgdata | The character array to store the message data in |
ulong hsk_can_fifo_getId | ( | const hsk_can_fifo | fifo | ) |
Returns the CAN ID of the selected FIFO entry.
fifo | The ID of the FIFO |
void hsk_can_fifo_next | ( | const hsk_can_fifo | fifo | ) |
Select the next FIFO entry.
The hsk_can_fifo_updated() and hsk_can_fifo_getData() functions always refer to a certain message within the FIFO. This function selects the next entry.
fifo | The ID of the FIFO to select the next entry from |
void hsk_can_fifo_setRxMask | ( | const hsk_can_fifo | fifo, |
ulong | msk | ||
) |
Changes the ID matching mask of an RX FIFO.
Every RX FIFO is setup to receive only on complete ID matches. This function allows updating the mask.
To generate a mask from a list of IDs use the following formula:
fifo | The FIFO to change the RX mask for |
msk | The bit mask to set for the FIFO |
void hsk_can_fifo_setupRx | ( | hsk_can_fifo | fifo, |
const ulong | id, | ||
const bool | extended, | ||
const ubyte | dlc | ||
) |
Set the FIFO up for receiving messages.
fifo | The FIFO to setup |
id | The message ID. |
extended | Set this to 1 for an extended CAN message |
dlc | The data length code, # of bytes in the message, valid values range from 0 to 8 |
bool hsk_can_fifo_updated | ( | const hsk_can_fifo | fifo | ) |
Return whether the currently selected FIFO entry was updated via CAN bus between this call and the previous call of this method.
It can be used to decide when to call hsk_can_fifo_getData() and hsk_can_fifo_next().
fifo | The identifier of the FIFO to check |
1 | The FIFO entry was updated since the last call of this function |
0 | The FIFO entry has not been updated since the last call of this function |
void hsk_can_init | ( | const ubyte | pins, |
const ulong | baud | ||
) |
Setup CAN communication with the desired baud rate.
The CAN node is chosen with the pin configuration.
The bus still needs to be enabled after being setup.
pins | Choose one of 7 CANn_IO_* configurations |
baud | The target baud rate to use |
Configure the Bit Timing Unit
One bit is s separated into 3 blocks, each of which are multiples of a time quantum. The size of the time quantum (TQ) is controlled by the BRP and DIV8 bits. Because TSYNC is fixed to a single quantum, the other segments should be made up of a minimum of TQs, so TSYNC doesn't get too short (making a bit up of more TQs requires each one to be shorter at the same baud rate). However, the minimum number of TQs is 8 and some spare quantums are needed to adjust the timing between each bit transmission.
Time Slice | Value | Minimum | Encoding |
---|---|---|---|
TSYNC | 1 | Fixed | Implicite |
TSEG1 | 8 | 3 | 7 |
TSEG2 | 3 | 2 | 2 |
SWJ | 4 | - | 3 |
The above values provide 4 time quantums to adjust between bits without dropping below 8 quantums. The adjustment value is provided with the SWJ time slice.
The sample point is between TSEG1 and TSEG2, i.e. at 75%.
This means one bit requires 12 cycles. The BRP bits can be used to achieve the desired baud rate:
The encoding of BRT is also VALUE+1.
I/O Configuration
There are 7 different I/O pin configurations, four are availabe to node 0 and three to node 1.
ubyte hsk_can_msg_connect | ( | const hsk_can_msg | msg, |
const hsk_can_node | node | ||
) |
Connect a message object to a CAN node.
msg | The identifier of the message object |
node | The CAN node to connect to |
CAN_ERROR | The given message is not valid |
0 | Success |
hsk_can_msg hsk_can_msg_create | ( | const ulong | id, |
const bool | extended, | ||
const ubyte | dlc | ||
) |
Creates a new CAN message.
Note that only up to 32 messages can exist at any given time.
Extended messages have 29 bit IDs and non-extended 11 bit IDs.
id | The message ID. |
extended | Set this to 1 for an extended CAN message. |
dlc | The data length code, # of bytes in the message, valid values range from 0 to 8. |
CAN_ERROR | Creating the message failed |
[0;32[ | A message identifier |
ubyte hsk_can_msg_delete | ( | const hsk_can_msg | msg | ) |
Delete a CAN message object.
msg | The identifier of the message object |
CAN_ERROR | The given message is not valid |
0 | Success |
ubyte hsk_can_msg_disconnect | ( | const hsk_can_msg | msg | ) |
Disconnect a CAN message object from its CAN node.
This takes a CAN message out of active communication, without deleting it.
msg | The identifier of the message object |
CAN_ERROR | The given message is not valid |
0 | Success |
void hsk_can_msg_getData | ( | const hsk_can_msg | msg, |
ubyte *const | msgdata | ||
) |
Gets the current data in the CAN message.
This writes DLC bytes from the CAN message object into msgdata.
msg | The identifier of the message object |
msgdata | The character array to store the message data in |
void hsk_can_msg_receive | ( | const hsk_can_msg | msg | ) |
Return the message into RX mode after sending a message.
After sending a message the messages with the same ID from other bus participants are ignored. This restores the original setting to receive messages.
msg | The identifier of the message to receive |
void hsk_can_msg_send | ( | const hsk_can_msg | msg | ) |
Request transmission of a message.
msg | The identifier of the message to send |
bool hsk_can_msg_sent | ( | const hsk_can_msg | msg | ) |
Return whether the message was successfully sent between this and the previous call of this method.
msg | The identifier of the message to check |
1 | The message was sent since the last call of this function |
0 | The message has not been sent since the last call of this function |
void hsk_can_msg_setData | ( | const hsk_can_msg | msg, |
const ubyte *const | msgdata | ||
) |
Sets the current data in the CAN message.
This writes DLC bytes from msgdata to the CAN message object.
msg | The identifier of the message object |
msgdata | The character array to get the message data from |
bool hsk_can_msg_updated | ( | const hsk_can_msg | msg | ) |
Return whether the message was updated via CAN bus between this call and the previous call of this method.
An update does not entail a change of message data. It just means the message was received on the CAN bus.
This is useful for cyclic message occurance checks.
msg | The identifier of the message to check |
1 | The message was updated since the last call of this function |
0 | The message has not been updated since the last call of this function |
ubyte hsk_can_status | ( | const hsk_can_node | node, |
const ubyte | field | ||
) |
Returns a status field of a CAN node.
node | The CAN node to return the status of |
field | The status field to select |