foxBMS  1.1.2
The foxBMS Battery Management System API Documentation
can_helper.c File Reference

Helper functions for the CAN module. More...

#include "can_helper.h"
#include "database.h"
#include "foxmath.h"
Include dependency graph for can_helper.c:

Go to the source code of this file.

Macros

#define CAN_SIGNAL_MAX_SIZE   (64u)
 
#define CAN_MESSAGE_BIT_MASK_ONE_BYTE   (0xFFu)
 
#define CAN_BYTE_LENGTH   (8u)
 

Functions

static uint64_t CAN_ConvertBitStartBigEndian (uint64_t bitStart, uint64_t bitLength)
 Convert bit start in big endian case. In the big endian case for CAN, the bit start is set to the MSB and the bit numbering is not directly usable. This functions converts the bit start to set it to the LSB of the signal and with the usual bit numbering. More...
 
void CAN_TxPrepareSignalData (float *pSignal, CAN_SIGNAL_TYPE_s signalProperties)
 Prepare signal data. This function takes the signal data and applies factor, applies offset and compares with signal minimum and maximum values. More...
 
void CAN_RxConvertRawSignalData (float *pSignalConverted, float signalRaw, CAN_SIGNAL_TYPE_s signalProperties)
 Convert raw signal data. This function takes the raw signal data and applies offset and factor to convert the raw value. More...
 
void CAN_TxSetMessageDataWithSignalData (uint64_t *pMessage, uint64_t bitStart, uint8_t bitLength, uint64_t canSignal, CAN_ENDIANNESS_e endianness)
 Puts CAN signal data in a 64-bit variable. This function is used to compose a 64-bit CAN message. It takes signal data, signal bit start, signal bit length and puts the data in the 64-bit variable. More...
 
void CAN_TxSetCanDataWithMessageData (uint64_t message, uint8_t *pCanData, CAN_ENDIANNESS_e endianness)
 Copy CAN data from a 64-bit variable to 8 bytes. This function is used to copy a 64-bit CAN message to 8 bytes. More...
 
void CAN_RxGetSignalDataFromMessageData (uint64_t message, uint64_t bitStart, uint8_t bitLength, uint64_t *pCanSignal, CAN_ENDIANNESS_e endianness)
 Gets CAN signal data from a 64-bit variable. This function is used to get signal data from a 64-bit CAN message. It takes signal bit start, signal bit length and extract signal data from the the 64-bit variable. More...
 
void CAN_RxGetMessageDataFromCanData (uint64_t *pMessage, const uint8_t *const kpkCanData, CAN_ENDIANNESS_e endianness)
 Copy CAN data from 8 bytes to a 64-bit variable. More...
 

Variables

static const uint8_t can_bigEndianTable [CAN_SIGNAL_MAX_SIZE]
 

Detailed Description

Helper functions for the CAN module.

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2021-04-22 (date of creation)
Updated
2021-07-29 (date of last update)
Prefix
CAN

Definition in file can_helper.c.

Macro Definition Documentation

◆ CAN_BYTE_LENGTH

#define CAN_BYTE_LENGTH   (8u)

length of one CAN byte in bit

Definition at line 69 of file can_helper.c.

◆ CAN_MESSAGE_BIT_MASK_ONE_BYTE

#define CAN_MESSAGE_BIT_MASK_ONE_BYTE   (0xFFu)

bitmask for extraction of one byte

Definition at line 66 of file can_helper.c.

◆ CAN_SIGNAL_MAX_SIZE

#define CAN_SIGNAL_MAX_SIZE   (64u)

Plausibility checking can signals lengths

Definition at line 63 of file can_helper.c.

Function Documentation

◆ CAN_ConvertBitStartBigEndian()

static uint64_t CAN_ConvertBitStartBigEndian ( uint64_t  bitStart,
uint64_t  bitLength 
)
static

Convert bit start in big endian case. In the big endian case for CAN, the bit start is set to the MSB and the bit numbering is not directly usable. This functions converts the bit start to set it to the LSB of the signal and with the usual bit numbering.

Parameters
bitStartbit start in big endian format
bitLengthsignal length
Returns
bit start position converted to little endian format

Example: big endian, bitStart = 53, bitLength = 13 For big endian, bitStart corresponds to MSB. First convert |07 06 05 04 03 02 01 00| to |63 62 61 60 59 58 57 56| |15 14 13 12 11 10 09 08| |55 54 53 52 51 50 49 48| |23 22 21 20 19 18 17 16| |47 46 45 44 43 42 41 40| |31 30 29 28 27 26 25 24| |39 38 37 36 35 34 33 32| |39 38 37 36 35 34 33 32| |31 30 29 28 27 26 25 24| |47 46 45 44 43 42 41 40| |23 22 21 20 19 18 17 16| |55 54 53 52 51 50 49 48| |15 14 13 12 11 10 09 08| |63 62 61 60 59 58 57 56| |07 06 05 04 03 02 01 00| to get MSB position in the usual bit representation (from 0 as LSB to 63 as MSB). In the example, 53 must be converted to 13.

Usual bit position of MSB of signal is now available. Now subtract signal length to get LSB position. In the example, bitStart is converted from 53 to 0 for the length of 13 bits. This corresponds to the usual data access in the little endian case, from bit 0 to bit 12, where bitStart corresponds to LSB.

Definition at line 97 of file can_helper.c.

◆ CAN_RxConvertRawSignalData()

void CAN_RxConvertRawSignalData ( float *  pSignalConverted,
float  signalRaw,
CAN_SIGNAL_TYPE_s  signalProperties 
)

Convert raw signal data. This function takes the raw signal data and applies offset and factor to convert the raw value.

Parameters
[out]pSignalConvertedcoverted signal data
[in]signalRawraw signal data
[in]signalPropertiessignal properties (factor, offset, min, max)

Definition at line 160 of file can_helper.c.

◆ CAN_RxGetMessageDataFromCanData()

void CAN_RxGetMessageDataFromCanData ( uint64_t *  pMessage,
const uint8_t *const  kpkCanData,
CAN_ENDIANNESS_e  endianness 
)

Copy CAN data from 8 bytes to a 64-bit variable.

This function is used to copy data from a 64-bit variable to 8 bytes.

Parameters
[in]pMessage64-bit where the data is copied
[out]kpkCanData8 bytes containing the data
[in]endiannessbig or little endianness of data

Definition at line 288 of file can_helper.c.

◆ CAN_RxGetSignalDataFromMessageData()

void CAN_RxGetSignalDataFromMessageData ( uint64_t  message,
uint64_t  bitStart,
uint8_t  bitLength,
uint64_t *  pCanSignal,
CAN_ENDIANNESS_e  endianness 
)

Gets CAN signal data from a 64-bit variable. This function is used to get signal data from a 64-bit CAN message. It takes signal bit start, signal bit length and extract signal data from the the 64-bit variable.

Parameters
[in]message64-bit variable containing the CAN data
[in]bitStartstarting bit were the signal data must be put
[in]bitLengthlength of the signal data to be put in CAN message
[out]pCanSignalsignal data to be retrieved from message
[in]endiannessbig or little endianness of data

Definition at line 248 of file can_helper.c.

Here is the call graph for this function:

◆ CAN_TxPrepareSignalData()

void CAN_TxPrepareSignalData ( float *  pSignal,
CAN_SIGNAL_TYPE_s  signalProperties 
)

Prepare signal data. This function takes the signal data and applies factor, applies offset and compares with signal minimum and maximum values.

Parameters
[in,out]pSignalsignal data
[in]signalPropertiessignal properties (factor, offset, min, max)

Definition at line 141 of file can_helper.c.

◆ CAN_TxSetCanDataWithMessageData()

void CAN_TxSetCanDataWithMessageData ( uint64_t  message,
uint8_t *  pCanData,
CAN_ENDIANNESS_e  endianness 
)

Copy CAN data from a 64-bit variable to 8 bytes. This function is used to copy a 64-bit CAN message to 8 bytes.

Parameters
[in]message64-bit variable containing the CAN data
[out]pCanData8 bytes where the data is copied
[in]endiannessbig or little endianness of data

Definition at line 205 of file can_helper.c.

◆ CAN_TxSetMessageDataWithSignalData()

void CAN_TxSetMessageDataWithSignalData ( uint64_t *  pMessage,
uint64_t  bitStart,
uint8_t  bitLength,
uint64_t  canSignal,
CAN_ENDIANNESS_e  endianness 
)

Puts CAN signal data in a 64-bit variable. This function is used to compose a 64-bit CAN message. It takes signal data, signal bit start, signal bit length and puts the data in the 64-bit variable.

Parameters
[out]pMessage64-bit variable containing the CAN data
[in]bitStartstarting bit were the signal data must be put
[in]bitLengthlength of the signal data to be put in CAN message
[in]canSignalsignal data to be put in message
[in]endiannessbig or little endianness of data

Definition at line 166 of file can_helper.c.

Here is the call graph for this function:

Variable Documentation

◆ can_bigEndianTable

const uint8_t can_bigEndianTable[CAN_SIGNAL_MAX_SIZE]
static
Initial value:
= {
56u, 57u, 58u, 59u, 60u, 61u, 62u, 63u, 48u, 49u, 50u, 51u, 52u, 53u, 54u, 55u, 40u, 41u, 42u, 43u, 44u, 45u,
46u, 47u, 32u, 33u, 34u, 35u, 36u, 37u, 38u, 39u, 24u, 25u, 26u, 27u, 28u, 29u, 30u, 31u, 16u, 17u, 18u, 19u,
20u, 21u, 22u, 23u, 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u, 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u}

To convert big endian startbit to usual little endian representation (from 0 as LSB to 63 as MSB)

Definition at line 74 of file can_helper.c.