58 #include "HL_system.h"
102 static bool I2C_WaitTransmit(i2cBASE_t *pI2cInterface, uint32_t timeout_us);
114 static bool I2C_WaitStop(i2cBASE_t *pI2cInterface, uint32_t timeout_us);
139 uint32_t i2cClock_khz = 0;
140 uint32_t prescaler = 0;
141 uint32_t wordTransmitTime_us = 0u;
142 uint8_t dFactor = 0u;
146 if (prescaler == 0u) {
148 }
else if (prescaler == 1u) {
156 (2u * (prescaler + 1u) * (pI2cInterface->CKH + dFactor));
158 return wordTransmitTime_us;
164 bool timeElapsed =
false;
167 while (((pI2cInterface->STR & (uint32_t)I2C_NACK_INT) == 0u) &&
168 ((pI2cInterface->STR & (uint32_t)I2C_TX_INT) == 0u) && (timeElapsed ==
false)) {
172 if (timeElapsed ==
true) {
179 static bool I2C_WaitStop(i2cBASE_t *pI2cInterface, uint32_t timeout_us) {
182 bool timeElapsed =
false;
185 while ((i2cIsStopDetected(pI2cInterface) == 0u) && (timeElapsed ==
false)) {
189 if (timeElapsed ==
true) {
203 return notifiedValueTx;
213 return notifiedValueRx;
227 i2cBASE_t *pI2cInterface,
228 uint32_t slaveAddress,
237 if ((pI2cInterface->STR & (uint32_t)I2C_BUSBUSY) == 0u) {
239 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
240 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
241 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
242 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
243 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
245 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
247 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
248 i2cSetDirection(pI2cInterface, (uint32_t)I2C_RECEIVER);
249 i2cSetSlaveAdd(pI2cInterface, slaveAddress);
250 i2cSetStart(pI2cInterface);
252 i2cSetStop(pI2cInterface);
255 for (uint16_t i = 0u; i < nrBytes; i++) {
257 if (((pI2cInterface->STR & (uint32_t)I2C_NACK_INT) == (uint32_t)I2C_NACK_INT) || (success ==
false)) {
258 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
259 i2cSetStop(pI2cInterface);
264 if (i == (nrBytes - 2u)) {
265 i2cSetStop(pI2cInterface);
270 if (success ==
false) {
272 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
273 i2cSetStop(pI2cInterface);
284 i2cBASE_t *pI2cInterface,
285 uint32_t slaveAddress,
287 uint8_t *writeData) {
294 if ((pI2cInterface->STR & (uint32_t)I2C_BUSBUSY) == 0u) {
296 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
297 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
298 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
299 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
300 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
302 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
303 i2cSetDirection(pI2cInterface, (uint32_t)I2C_TRANSMITTER);
304 i2cSetSlaveAdd(pI2cInterface, slaveAddress);
305 i2cSetStop(pI2cInterface);
306 i2cSetCount(pI2cInterface, nrBytes);
307 i2cSetStart(pI2cInterface);
310 for (uint16_t i = 0u; i < nrBytes; i++) {
311 pI2cInterface->DXR = (uint32_t)writeData[i];
313 if (((pI2cInterface->STR & (uint32_t)I2C_NACK_INT) == (uint32_t)I2C_NACK_INT) || (success ==
false)) {
314 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
315 i2cSetStop(pI2cInterface);
322 if (success ==
false) {
324 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
325 i2cSetStop(pI2cInterface);
336 i2cBASE_t *pI2cInterface,
337 uint32_t slaveAddress,
338 uint32_t nrBytesWrite,
340 uint32_t nrBytesRead,
350 if ((pI2cInterface->STR & (uint32_t)I2C_BUSBUSY) == 0u) {
352 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
353 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
354 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
355 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
356 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
358 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
359 i2cSetDirection(pI2cInterface, (uint32_t)I2C_TRANSMITTER);
360 i2cSetSlaveAdd(pI2cInterface, slaveAddress);
361 i2cSetStart(pI2cInterface);
366 for (uint16_t i = 0u; i < nrBytesWrite; i++) {
367 pI2cInterface->DXR = (uint32_t)writeData[i];
369 if (((pI2cInterface->STR & (uint32_t)I2C_NACK_INT) == (uint32_t)I2C_NACK_INT) || (success ==
false)) {
370 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
371 i2cSetStop(pI2cInterface);
377 if (!(((pI2cInterface->STR & (uint32_t)I2C_NACK_INT) == (uint32_t)I2C_NACK_INT) || (success ==
false))) {
378 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
379 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
380 i2cSetDirection(pI2cInterface, (uint32_t)I2C_RECEIVER);
381 i2cSetStart(pI2cInterface);
382 if (nrBytesRead == 1u) {
383 i2cSetStop(pI2cInterface);
386 for (uint16_t i = 0u; i < nrBytesRead; i++) {
388 if (((pI2cInterface->STR & (uint32_t)I2C_NACK_INT) == (uint32_t)I2C_NACK_INT) || (success ==
false)) {
389 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
390 i2cSetStop(pI2cInterface);
395 if (i == (nrBytesRead - 2u)) {
396 i2cSetStop(pI2cInterface);
402 if (success ==
false) {
404 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
405 i2cSetStop(pI2cInterface);
416 i2cBASE_t *pI2cInterface,
417 uint32_t slaveAddress,
428 if ((pI2cInterface->STR & (uint32_t)I2C_BUSBUSY) == 0u) {
430 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
431 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
432 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
433 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
434 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
437 dmaChannel_t channelRx = DMA_CH0;
438 if (pI2cInterface == i2cREG1) {
440 }
else if (pI2cInterface == i2cREG2) {
455 dmaRAMREG->PCP[(dmaChannel_t)channelRx].IDADDR = (uint32_t)readData;
460 dmaSetChEnable((dmaChannel_t)channelRx, (dmaTriggerType_t)DMA_HW);
468 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
469 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
470 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
472 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
473 i2cSetDirection(pI2cInterface, (uint32_t)I2C_RECEIVER);
474 i2cSetSlaveAdd(pI2cInterface, slaveAddress);
475 i2cSetStart(pI2cInterface);
482 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
483 i2cSetStop(pI2cInterface);
487 if (pI2cInterface == i2cREG1) {
489 }
else if (pI2cInterface == i2cREG2) {
496 if (success ==
false) {
498 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
499 i2cSetStop(pI2cInterface);
511 i2cBASE_t *pI2cInterface,
512 uint32_t slaveAddress,
514 uint8_t *writeData) {
523 if ((pI2cInterface->STR & (uint32_t)I2C_BUSBUSY) == 0u) {
524 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
525 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
526 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
527 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
528 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
531 dmaChannel_t channelTx = DMA_CH0;
532 if (pI2cInterface == i2cREG1) {
534 }
else if (pI2cInterface == i2cREG2) {
549 dmaRAMREG->PCP[(dmaChannel_t)channelTx].ISADDR = (uint32_t)writeData;
554 dmaSetChEnable((dmaChannel_t)channelTx, (dmaTriggerType_t)DMA_HW);
561 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
562 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
563 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
564 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
565 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
567 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
568 i2cSetDirection(pI2cInterface, (uint32_t)I2C_TRANSMITTER);
569 i2cSetSlaveAdd(pI2cInterface, slaveAddress);
570 i2cSetStop(pI2cInterface);
571 i2cSetCount(pI2cInterface, nrBytes);
573 i2cSetStart(pI2cInterface);
580 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
581 i2cSetStop(pI2cInterface);
585 if (success ==
false) {
587 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
588 i2cSetStop(pI2cInterface);
600 i2cBASE_t *pI2cInterface,
601 uint32_t slaveAddress,
602 uint32_t nrBytesWrite,
604 uint32_t nrBytesRead,
613 dmaChannel_t channelRx = DMA_CH0;
614 dmaChannel_t channelTx = DMA_CH0;
618 if ((pI2cInterface->STR & (uint32_t)I2C_BUSBUSY) == 0u) {
622 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
623 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
624 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
625 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
626 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
629 if (pI2cInterface == i2cREG1) {
631 }
else if (pI2cInterface == i2cREG2) {
646 dmaRAMREG->PCP[(dmaChannel_t)channelTx].ISADDR = (uint32_t)writeData;
651 dmaSetChEnable((dmaChannel_t)channelTx, (dmaTriggerType_t)DMA_HW);
658 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
659 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
660 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
661 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
662 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
664 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
665 i2cSetDirection(pI2cInterface, (uint32_t)I2C_TRANSMITTER);
666 i2cSetSlaveAdd(pI2cInterface, slaveAddress);
668 i2cSetStart(pI2cInterface);
675 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
676 i2cSetStop(pI2cInterface);
682 if (pI2cInterface == i2cREG1) {
684 }
else if (pI2cInterface == i2cREG2) {
699 dmaRAMREG->PCP[(dmaChannel_t)channelRx].IDADDR = (uint32_t)readData;
702 dmaRAMREG->PCP[(dmaChannel_t)channelRx].ITCOUNT =
705 dmaSetChEnable((dmaChannel_t)channelRx, (dmaTriggerType_t)DMA_HW);
718 pI2cInterface->MDR &= ~((uint32_t)I2C_STOP_COND);
719 pI2cInterface->MDR &= ~((uint32_t)I2C_START_COND);
720 pI2cInterface->MDR &= ~((uint32_t)I2C_REPEATMODE);
721 pI2cInterface->STR |= (uint32_t)I2C_TX_INT;
722 pI2cInterface->STR |= (uint32_t)I2C_RX_INT;
726 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
727 i2cSetMode(pI2cInterface, (uint32_t)I2C_MASTER);
728 i2cSetDirection(pI2cInterface, (uint32_t)I2C_RECEIVER);
729 i2cSetStart(pI2cInterface);
737 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
738 i2cSetStop(pI2cInterface);
742 if (pI2cInterface == i2cREG1) {
744 }
else if (pI2cInterface == i2cREG2) {
751 if (success ==
false) {
753 pI2cInterface->MDR |= (uint32_t)I2C_REPEATMODE;
754 i2cSetStop(pI2cInterface);
775 bool timeElapsed =
false;
778 while (((pI2cInterface->STR & (uint32_t)I2C_RX_INT) == 0u) && (timeElapsed ==
false)) {
782 if (timeElapsed ==
true) {
790 #ifdef UNITY_UNIT_TEST
Headers for the driver for the DMA module.
#define DMA_CHANNEL_I2C1_TX
#define DMA_CHANNEL_I2C2_TX
#define DMA_CHANNEL_I2C2_RX
#define DMA_INITIAL_FRAME_COUNTER_POSITION
#define DMA_CHANNEL_I2C1_RX
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition of foxBMS standard types.
#define NULL_PTR
Null pointer.
Function to switch between user mode and privilege mode.
#define FSYS_SwitchToUserMode()
Switch back to user mode.
long FSYS_RaisePrivilege(void)
Raise privilege.
static uint32_t I2C_WaitForTxCompletedNotification(void)
Wait for the I2C transmit communication to complete, using notifications.
STD_RETURN_TYPE_e I2C_WriteRead(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytesWrite, uint8_t *writeData, uint32_t nrBytesRead, uint8_t *readData)
reads from an I2C slave, blocking.
bool I2C_WaitReceive(i2cBASE_t *pI2cInterface, uint32_t timeout_us)
Waits for the I2C Rx buffer to be full.
uint8_t i2c_rxLastByteInterface2
uint8_t i2c_rxLastByteInterface1
static bool I2C_WaitTransmit(i2cBASE_t *pI2cInterface, uint32_t timeout_us)
Waits for the I2C Tx buffer to be empty.
static bool I2C_WaitStop(i2cBASE_t *pI2cInterface, uint32_t timeout_us)
Waits for a stop condition to be detected.
static uint32_t I2C_GetWordTransmitTime(i2cBASE_t *pI2cInterface)
Return transmit time of a word in microseconds.
uint8_t I2C_ReadLastRxByte(i2cBASE_t *pI2cInterface)
Used to read last byte received per I2C.
STD_RETURN_TYPE_e I2C_ReadDma(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, no register address written first, using DMA.
STD_RETURN_TYPE_e I2C_Read(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, no register address written first, blocking.
STD_RETURN_TYPE_e I2C_WriteReadDma(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytesWrite, uint8_t *writeData, uint32_t nrBytesRead, uint8_t *readData)
reads from an I2C slave, using DMA.
static uint32_t I2C_WaitForRxCompletedNotification(void)
Wait for the I2C receive communication to complete, using notifications.
static void I2C_ClearNotifications(void)
Clear pending notifications.
STD_RETURN_TYPE_e I2C_Write(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, no register address written first, blocking.
STD_RETURN_TYPE_e I2C_WriteDma(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, no register address written first, using DMA.
void I2C_Initialize(void)
Initialize the I2C hardware with dedicated HAL functions. Has to be called before any call to the res...
Header for the driver for the I2C module.
#define I2C_NOTIFICATION_RX_INDEX
#define I2C_FACTOR_WORD_TO_BITS
#define I2C_NOTIFICATION_TX_INDEX
#define I2C_DFACTOR_VALUE_PRESCALER_0
#define I2C_TX_TIME_MARGIN_us
#define I2C_DFACTOR_VALUE_PRESCALER_OTHER
#define I2C_TX_NOTIFIED_VALUE
#define I2C_FACTOR_S_TO_US
#define I2C_FACTOR_MHZ_TO_HZ
#define I2C_NOTIFICATION_TIMEOUT_ms
#define I2C_DDR_REGISTER_DATA_MASK
#define I2C_RX_NOTIFIED_VALUE
#define I2C_DFACTOR_VALUE_PRESCALER_1
#define I2C_NO_NOTIFIED_VALUE
#define I2C_PRESCALER_MASK
uint32_t MCU_GetFreeRunningCount(void)
Get the current value of the Free Running Counter 0 (FRC0)
void MCU_Delay_us(uint32_t delay_us)
Wait blocking a certain time in microseconds.
bool MCU_IsTimeElapsed(uint32_t startCounter, uint32_t timeout_us)
Checks if a timeout in microseconds has elapsed.
Headers for the driver for the MCU module.
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.