54 #define N775_WRITE_SPI_BUFFER_SIZE (4u)
56 #define N775_READ_HEADER_SPI_BUFFER_SIZE (3u)
61 #define N775_READ_PAYLOAD_SPI_BUFFER_SIZE (4u)
69 #define N775_MAX_ANSWER_FRAMES (30u)
71 #define N775_WAIT_TIME_AFTER_WRITE_US (5u)
73 #define N775_WAIT_TIME_AFTER_READ_US (5u)
75 #define N775_SPI_WRITE_TIMEOUT_US (500u)
80 #define N775_SPI_READ_TIMEOUT_US (2000u)
85 #pragma SET_DATA_SECTION(".sharedRAM")
94 #pragma SET_DATA_SECTION()
127 pBuffer[0u] = message.head;
128 pBuffer[1u] = message.data.dhead;
129 pBuffer[2u] = message.data.data[0u];
130 pBuffer[3u] = message.crc;
140 return notifiedValueTx;
150 return notifiedValueRx;
156 uint16_t deviceAddress,
157 uint16_t registerAddress,
161 uc_msg_t message = {0};
168 BMS1_CMD_WRITE, 0, (deviceAddress | (1u << 6u)), registerAddress, 0, &value, &message);
196 uint16_t deviceAddress,
197 uint16_t registerAddress,
208 uint16_t deviceAddress,
209 uint16_t numberOfItems,
210 uint16_t responseLength,
211 uint16_t registerAddress,
220 uint16_t itemsReadRemaining = numberOfItems;
221 uint16_t *pReadValues = pValues;
235 uc_msg_t txMessage = {0u};
236 uc_msg_t rxMessage = {0u};
239 uint16_t read_parameter;
242 uint16_t rsp_mst_addr;
243 uint16_t rsp_dev_addr;
244 uint16_t rsp_reg_addr;
246 uint16_t rsp_values[4];
248 read_parameter = ((uint16_t)1u << 10u) +
249 ((responseLength - 1u) << 8u) +
250 ((numberOfItems - 1u) << 0u);
253 BMS1_CMD_READ, 0, (deviceAddress | (1u << 6u)), registerAddress, 0, &read_parameter, &txMessage);
266 uint16_t nrAnswerFrames = (((numberOfItems - 1u) + 1u) / ((responseLength - 1u) + 1u));
273 if ((((numberOfItems - 1u) + 1u) % ((responseLength - 1u) + 1u)) != 0u) {
294 bool n775_rxCompleted =
true;
298 n775_rxCompleted =
false;
306 if (n775_rxCompleted ==
false) {
311 for (uint16_t i = 0; i < nrAnswerFrames; i++) {
313 rxMessage.message_length = 4u + (responseLength - 1u);
318 rxMessage.data.data[0u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 2u];
319 if (rxMessage.message_length <= 4u) {
322 if (rxMessage.message_length == 5u) {
323 rxMessage.data.data[1u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 3u];
326 if (rxMessage.message_length == 6u) {
327 rxMessage.data.data[1u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 3u];
328 rxMessage.data.data[2u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 4u];
331 rxMessage.data.data[1u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 3u];
332 rxMessage.data.data[2u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 4u];
333 rxMessage.data.data[3u] =
n775FromTplRxBuffer[4u + (i * (4u + (responseLength - 1u))) + 5u];
351 if (rsp_dev_addr == (deviceAddress | (1u << 6u))) {
352 if (rsp_reg_addr == (registerAddress + (i * ((responseLength - 1u) + 1u)))) {
353 for (uint16_t j = 0u; j < (rsp_length + 1u); j++) {
357 if (itemsReadRemaining > 0u) {
358 *pReadValues = rsp_values[j];
360 itemsReadRemaining--;
375 return communicationStatus;
388 uint16_t masterAddress,
389 uint16_t deviceAddress,
390 uint16_t registerAddress,
393 uc_msg_t *pMessage) {
397 set_cmd(pMessage, cmd);
398 set_madd(pMessage, masterAddress);
399 set_cadd(pMessage, deviceAddress >> 6u);
400 set_devadd(pMessage, deviceAddress & 0x3Fu);
401 set_msgcnt(pMessage, 0);
402 set_datalen(pMessage, length);
403 set_regadd(pMessage, registerAddress);
404 for (uint16_t i = 0; i <= length; i++) {
405 set_data(pMessage, pValue[i], i);
407 set_message_length(pMessage, length + 4u);
410 set_crc(pMessage, calc_crc(pMessage));
417 uint16_t *pMasterAddress,
418 uint16_t *pDeviceAddress,
419 uint16_t *pRegisterAddress,
430 uint16_t messageLength;
431 uint16_t messageCount;
432 uint16_t chainAddress;
433 uint16_t deviceAddressInChain;
435 bool errorCodeMatch =
false;
440 get_message_length(pMessage, &messageLength);
441 if (messageLength == 0u) {
442 errorCodeMatch =
true;
446 if ((errorCodeMatch ==
false) && (messageLength < 4u)) {
447 errorCodeMatch =
true;
453 if ((errorCodeMatch ==
false) && (!check_crc(pMessage, calc_crc(pMessage)))) {
454 errorCodeMatch =
true;
455 uint16_t receivedCrc;
456 get_crc(pMessage, &receivedCrc);
461 get_cmd(pMessage, pCommand);
462 get_madd(pMessage, pMasterAddress);
463 get_cadd(pMessage, &chainAddress);
464 get_devadd(pMessage, &deviceAddressInChain);
465 *pDeviceAddress = (chainAddress << 6u) | deviceAddressInChain;
466 get_msgcnt(pMessage, &messageCount);
467 get_datalen(pMessage, pLength);
468 get_regadd(pMessage, pRegisterAddress);
473 errorCodeMatch =
true;
486 if (errorCodeMatch ==
false) {
487 for (
int i = 0u; i <= *pLength; i++) {
488 get_data(pMessage, &pValue[i], i);
490 if (*pCommand != BMS1_CMD_RESP) {
498 return communicationStatus;
502 #ifdef UNITY_UNIT_TEST
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
Headers for the driver for the DMA module.
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
#define NULL_PTR
Null pointer.
Header for the driver for the IO module.
Headers for the driver for the MCU module.
#define N775_READ_HEADER_SPI_BUFFER_SIZE
N775_COMMUNICATION_STATUS_e N775_CommunicationDecomposeMessage(uc_msg_t *pMessage, uint16_t *pCommand, uint16_t *pMasterAddress, uint16_t *pDeviceAddress, uint16_t *pRegisterAddress, uint16_t *pLength, uint16_t *pValue, uint8_t string)
Decomposes and analysis a message.
static uint32_t N775_WaitForTxCompletedNotification(void)
Wait for the SPI transmit communication to complete, using notifications.
static uint16_t n775FromTplRxBuffer[N775_WRITE_SPI_BUFFER_SIZE+((N775_READ_HEADER_SPI_BUFFER_SIZE+N775_READ_PAYLOAD_SPI_BUFFER_SIZE) *N775_MAX_ANSWER_FRAMES)]
N775_COMMUNICATION_STATUS_e N775_CommunicationRead(uint16_t deviceAddress, uint16_t registerAddress, uint16_t *pValue, N775_STATE_s *pState)
Read a value from a specific register in a specific device.
static uint16_t referenceMessageCounter[BS_NR_OF_STRINGS][512]
static uint16_t n775ToTplTxBuffer[N775_WRITE_SPI_BUFFER_SIZE]
static uint16_t n775ToTplRxBuffer[N775_WRITE_SPI_BUFFER_SIZE]
#define N775_READ_PAYLOAD_SPI_BUFFER_SIZE
static uint32_t N775_WaitForRxCompletedNotification(void)
Wait for the SPI receive communication to complete, using notifications.
N775_COMMUNICATION_STATUS_e N775_CommunicationReadMultiple(uint16_t deviceAddress, uint16_t numberOfItems, uint16_t responseLength, uint16_t registerAddress, uint16_t *pValues, N775_STATE_s *pState)
Read multiple values from specific registers in a specific device.
void N775_ResetMessageCounter(uint16_t deviceAddress, uint8_t string)
Reset the message counter for one or all devices.
static void N775_ConvertMessageToBuffer(uint16_t *pBuffer, uc_msg_t message)
Copies a message to the buffer to be passed to the SPI transmit functions.
void N775_CommunicationWrite(uint16_t deviceAddress, uint16_t registerAddress, uint16_t value, SPI_INTERFACE_CONFIG_s *pSpiInterface)
Write a value into a specific register in a specific device.
#define N775_WRITE_SPI_BUFFER_SIZE
static uint16_t n775FromTplTxBuffer[N775_WRITE_SPI_BUFFER_SIZE+((N775_READ_HEADER_SPI_BUFFER_SIZE+N775_READ_PAYLOAD_SPI_BUFFER_SIZE) *N775_MAX_ANSWER_FRAMES)]
#define N775_MAX_ANSWER_FRAMES
void N775_CommunicationComposeMessage(uint16_t cmd, uint16_t masterAddress, uint16_t deviceAddress, uint16_t registerAddress, uint16_t length, uint16_t *pValue, uc_msg_t *pMessage)
Composes a message.
Header for the low level driver for the MC33775A.
enum N775_COMMUNICATION_STATUS N775_COMMUNICATION_STATUS_e
@ N775_COMMUNICATION_ERROR_NOT_MATCHING_REGISTER_ADDRESS
@ N775_COMMUNICATION_ERROR_NO_ACCESS
@ N775_COMMUNICATION_ERROR_WRONG_MESSAGE_COUNT
@ N775_COMMUNICATION_ERROR_WRONG_CRC
@ N775_COMMUNICATION_ERROR_TIMEOUT
@ N775_COMMUNICATION_ERROR_SHORT_MESSAGE
@ N775_COMMUNICATION_ERROR_NO_RESPONSE
@ N775_COMMUNICATION_ERROR_NOT_MATCHING_DEVICE_ADDRESS
#define N775_ERROR_REGISTER_ADDRESS
#define N775_NOTIFICATION_RX_TIMEOUT_ms
#define N775_NOTIFICATION_TX_TIMEOUT_ms
#define N775_NO_NOTIFIED_VALUE
#define N775_RX_NOTIFIED_VALUE
#define N775_NOTIFICATION_RX_INDEX
#define N775_NOTIFICATION_TX_INDEX
#define N775_TX_NOTIFIED_VALUE
Declaration of the OS wrapper interface.
OS_STD_RETURN_e OS_ClearNotificationIndexed(uint32_t indexToClear)
Clear pending notification of a task, with index.
OS_STD_RETURN_e OS_WaitForNotificationIndexed(uint32_t indexToWaitOn, uint32_t *pNotifiedValue, uint32_t timeout)
Wait for a notification, with index.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
STD_RETURN_TYPE_e SPI_TransmitReceiveDataDma(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
Transmits and receives data on SPI with DMA.
STD_RETURN_TYPE_e SPI_SlaveSetReceiveDataDma(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
Transmits and receives data on SPI with DMA.
uint8_t SPI_GetSpiIndex(spiBASE_t *pNode)
Returns index of SPI node.
Headers for the driver for the SPI module.
SPI_BUSY_STATE_e spi_busyFlags[]
Headers for the configuration for the SPI module.
SPI_INTERFACE_CONFIG_s * pSpiTxSequence
SPI_INTERFACE_CONFIG_s * pSpiRxSequence