foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
i2c.c File Reference

Driver for the I2C module. More...

#include "i2c.h"
#include "HL_system.h"
#include "database.h"
#include "diag.h"
#include "dma.h"
#include "fstd_types.h"
#include "fsystem.h"
#include "mcu.h"
#include "os.h"
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for i2c.c:

Go to the source code of this file.

Functions

static uint32_t I2C_GetWordTransmitTime (i2cBASE_t *pI2cInterface)
 Return transmit time of a word in microseconds. More...
 
static bool I2C_WaitTransmit (i2cBASE_t *pI2cInterface, uint32_t timeout_us)
 Waits for the I2C Tx buffer to be empty. More...
 
static bool I2C_WaitStop (i2cBASE_t *pI2cInterface, uint32_t timeout_us)
 Waits for a stop condition to be detected. More...
 
static uint32_t I2C_WaitForTxCompletedNotification (void)
 Wait for the I2C transmit communication to complete, using notifications. More...
 
static uint32_t I2C_WaitForRxCompletedNotification (void)
 Wait for the I2C receive communication to complete, using notifications. More...
 
static void I2C_ClearNotifications (void)
 Clear pending notifications. More...
 
void I2C_Initialize (void)
 Initialize the I2C hardware with dedicated HAL functions. Has to be called before any call to the rest of this API. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
uint8_t I2C_ReadLastRxByte (i2cBASE_t *pI2cInterface)
 Used to read last byte received per I2C. More...
 
bool I2C_WaitReceive (i2cBASE_t *pI2cInterface, uint32_t timeout_us)
 Waits for the I2C Rx buffer to be full. More...
 

Variables

uint8_t i2c_rxLastByteInterface1 = 0u
 
uint8_t i2c_rxLastByteInterface2 = 0u
 

Detailed Description

Driver for the I2C 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-07-22 (date of creation)
Updated
2023-10-12 (date of last update)
Version
v1.6.0
Prefix
I2C

Definition in file i2c.c.

Function Documentation

◆ I2C_ClearNotifications()

static void I2C_ClearNotifications ( void  )
static

Clear pending notifications.

Definition at line 216 of file i2c.c.

Here is the call graph for this function:

◆ I2C_GetWordTransmitTime()

static uint32_t I2C_GetWordTransmitTime ( i2cBASE_t *  pI2cInterface)
static

Return transmit time of a word in microseconds.

The function uses the clock settings of the interface to determine the time needed to transmit one word. Word means one byte + the ACK bit.

Parameters
pI2cInterfaceI2C interface to use
Returns
time in microseconds needed to transmit a byte on the I2C interface

Definition at line 137 of file i2c.c.

◆ I2C_Initialize()

void I2C_Initialize ( void  )

Initialize the I2C hardware with dedicated HAL functions. Has to be called before any call to the rest of this API.

Definition at line 222 of file i2c.c.

◆ I2C_Read()

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.

Parameters
pI2cInterfaceI2C interface to use
slaveAddressaddress of slave to communicate with
nrBytesnumber of registers to read
readDatabuffer containing the read data
Returns
STD_OK if transmission OK, STD_NOT_OK otherwise

Definition at line 226 of file i2c.c.

Here is the call graph for this function:

◆ I2C_ReadDma()

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.

Parameters
pI2cInterfaceI2C interface to use
slaveAddressaddress of slave to communicate with
nrBytesnumber of registers to read
readDatabuffer containing the read data
Returns
STD_OK if transmission OK, STD_NOT_OK otherwise

Definition at line 415 of file i2c.c.

Here is the call graph for this function:

◆ I2C_ReadLastRxByte()

uint8_t I2C_ReadLastRxByte ( i2cBASE_t *  pI2cInterface)

Used to read last byte received per I2C.

This function must be called in the DMA Rx callback. It returns the last byte received per I2C

Parameters
pI2cInterfaceI2C interface used
Returns
last byte received on the I2C bus

Definition at line 766 of file i2c.c.

◆ I2C_WaitForRxCompletedNotification()

static uint32_t I2C_WaitForRxCompletedNotification ( void  )
static

Wait for the I2C receive communication to complete, using notifications.

Returns
I2C_RX_NOTIFIED_VALUE if notification received, I2C_NO_NOTIFIED_VALUE if timeout reached

Suspend task and wait for I2C DMA RX finished notification, clear notification value on entry and exit

Definition at line 206 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WaitForTxCompletedNotification()

static uint32_t I2C_WaitForTxCompletedNotification ( void  )
static

Wait for the I2C transmit communication to complete, using notifications.

Returns
I2C_TX_NOTIFIED_VALUE if notification received, I2C_NO_NOTIFIED_VALUE if timeout reached

Suspend task and wait for I2C DMA TX finished notification, clear notification value on entry and exit

Definition at line 196 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WaitReceive()

bool I2C_WaitReceive ( i2cBASE_t *  pI2cInterface,
uint32_t  timeout_us 
)

Waits for the I2C Rx buffer to be full.

When the buffer is full, the received byte can be read. If the buffer is full before timeout_us microseconds are elapsed, the function returns true, false otherwise. The function also returns false if a NACK condition is detected.

Parameters
pI2cInterfaceI2C interface to use
timeout_ustime in microseconds to wait until the buffer is full
Returns
true if buffer is full within timeout, false otherwise

Definition at line 772 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WaitStop()

static bool I2C_WaitStop ( i2cBASE_t *  pI2cInterface,
uint32_t  timeout_us 
)
static

Waits for a stop condition to be detected.

When a stop condition is issued, this function waits until the stop condition is detected on the bus. This means that that transmission is finished. If stop is detected before timeout_us microseconds are elapsed, the function returns true, false otherwise.

Parameters
pI2cInterfaceI2C interface to use
timeout_ustime in microseconds to wait until stop is detected
Returns
true if stop is detected within timeout, false otherwise

Definition at line 179 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WaitTransmit()

static bool I2C_WaitTransmit ( i2cBASE_t *  pI2cInterface,
uint32_t  timeout_us 
)
static

Waits for the I2C Tx buffer to be empty.

When the buffer is empty, the next byte can be sent. If the buffer is empty before timeout_us microseconds are elapsed, the function returns true, false otherwise. The function also returns false if a NACK condition is detected.

Parameters
pI2cInterfaceI2C interface to use
timeout_ustime in microseconds to wait until the buffer is empty
Returns
true if buffer is empty within timeout, false otherwise

Definition at line 161 of file i2c.c.

Here is the call graph for this function:

◆ I2C_Write()

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.

Parameters
pI2cInterfaceI2C interface to use
slaveAddressaddress of slave to communicate with
nrBytesnumber of registers to write
writeDatabuffer containing the data to write
Returns
STD_OK if transmission OK, STD_NOT_OK otherwise

Definition at line 283 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WriteDma()

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.

Parameters
pI2cInterfaceI2C interface to use
slaveAddressaddress of slave to communicate with
nrBytesnumber of registers to write
writeDatabuffer containing the data to write
Returns
retVal STD_OK if transmission OK, STD_NOT_OK otherwise

Definition at line 510 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WriteRead()

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.

Parameters
pI2cInterfaceI2C interface to use
slaveAddressaddress of slave to communicate with
nrBytesWritenumber of registers to read
writeDatabuffer containing the read data
nrBytesReadnumber of registers to read
readDatabuffer containing the read data
Returns
STD_OK if transmission OK, STD_NOT_OK otherwise

Definition at line 335 of file i2c.c.

Here is the call graph for this function:

◆ I2C_WriteReadDma()

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.

Parameters
pI2cInterfaceI2C interface to use
slaveAddressaddress of slave to communicate with
nrBytesWritenumber of registers to read
writeDatabuffer containing the read data
nrBytesReadnumber of registers to read
readDatabuffer containing the read data
Returns
STD_OK if transmission OK, STD_NOT_OK otherwise

Definition at line 599 of file i2c.c.

Here is the call graph for this function:

Variable Documentation

◆ i2c_rxLastByteInterface1

uint8_t i2c_rxLastByteInterface1 = 0u

Definition at line 75 of file i2c.c.

◆ i2c_rxLastByteInterface2

uint8_t i2c_rxLastByteInterface2 = 0u

Definition at line 77 of file i2c.c.