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

Header for the driver for the I2C module. More...

#include "HL_i2c.h"
#include "fstd_types.h"
#include <stdint.h>
Include dependency graph for i2c.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define I2C_TIMEOUT_us   (1000u)
 
#define I2C_TXDMAEN   (0x2u)
 
#define I2C_RXDMAEN   (0x1u)
 
#define I2C_DDR_REGISTER_DATA_MASK   (0xFFu)
 
#define I2C_TASK_HANDLE   (ftsk_taskHandleI2c)
 
#define I2C_NOTIFICATION_TX_INDEX   (1u)
 
#define I2C_NOTIFICATION_RX_INDEX   (2u)
 
#define I2C_NO_NOTIFIED_VALUE   (0x0u)
 
#define I2C_TX_NOTIFIED_VALUE   (0x51u)
 
#define I2C_RX_NOTIFIED_VALUE   (0x61u)
 
#define I2C_RX_NOTCOME_VALUE   (0x62u)
 
#define I2C_NOTIFICATION_TIMEOUT_ms   (2u)
 
#define I2C_PRESCALER_MASK   (0xFFu)
 
#define I2C_FACTOR_MHZ_TO_HZ   (1000000u)
 
#define I2C_FACTOR_S_TO_US   (1000000u)
 
#define I2C_FACTOR_WORD_TO_BITS   (9u)
 
#define I2C_TX_TIME_MARGIN_us   (5u)
 
#define I2C_DFACTOR_VALUE_PRESCALER_0   (7u)
 
#define I2C_DFACTOR_VALUE_PRESCALER_1   (6u)
 
#define I2C_DFACTOR_VALUE_PRESCALER_OTHER   (5u)
 

Functions

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
 
uint8_t i2c_rxLastByteInterface2
 

Detailed Description

Header for the 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.h.

Macro Definition Documentation

◆ I2C_DDR_REGISTER_DATA_MASK

#define I2C_DDR_REGISTER_DATA_MASK   (0xFFu)

Definition at line 77 of file i2c.h.

◆ I2C_DFACTOR_VALUE_PRESCALER_0

#define I2C_DFACTOR_VALUE_PRESCALER_0   (7u)

Value of d factor in I2C configuration, depends on prescaler value See Technical Reference Manual p.1769 eq.65, (SPNU563A - March 2018)

Definition at line 112 of file i2c.h.

◆ I2C_DFACTOR_VALUE_PRESCALER_1

#define I2C_DFACTOR_VALUE_PRESCALER_1   (6u)

Value of d factor in I2C configuration, depends on prescaler value See Technical Reference Manual p.1769 eq.65, (SPNU563A - March 2018)

Definition at line 113 of file i2c.h.

◆ I2C_DFACTOR_VALUE_PRESCALER_OTHER

#define I2C_DFACTOR_VALUE_PRESCALER_OTHER   (5u)

Value of d factor in I2C configuration, depends on prescaler value See Technical Reference Manual p.1769 eq.65, (SPNU563A - March 2018)

Definition at line 114 of file i2c.h.

◆ I2C_FACTOR_MHZ_TO_HZ

#define I2C_FACTOR_MHZ_TO_HZ   (1000000u)

Definition at line 102 of file i2c.h.

◆ I2C_FACTOR_S_TO_US

#define I2C_FACTOR_S_TO_US   (1000000u)

Definition at line 104 of file i2c.h.

◆ I2C_FACTOR_WORD_TO_BITS

#define I2C_FACTOR_WORD_TO_BITS   (9u)

Definition at line 106 of file i2c.h.

◆ I2C_NO_NOTIFIED_VALUE

#define I2C_NO_NOTIFIED_VALUE   (0x0u)

Definition at line 87 of file i2c.h.

◆ I2C_NOTIFICATION_RX_INDEX

#define I2C_NOTIFICATION_RX_INDEX   (2u)

Definition at line 85 of file i2c.h.

◆ I2C_NOTIFICATION_TIMEOUT_ms

#define I2C_NOTIFICATION_TIMEOUT_ms   (2u)

Time in ms to wait for DMA notification, after this delay the transaction is considered to have failed

Definition at line 98 of file i2c.h.

◆ I2C_NOTIFICATION_TX_INDEX

#define I2C_NOTIFICATION_TX_INDEX   (1u)

Definition at line 83 of file i2c.h.

◆ I2C_PRESCALER_MASK

#define I2C_PRESCALER_MASK   (0xFFu)

Definition at line 100 of file i2c.h.

◆ I2C_RX_NOTCOME_VALUE

#define I2C_RX_NOTCOME_VALUE   (0x62u)

Definition at line 93 of file i2c.h.

◆ I2C_RX_NOTIFIED_VALUE

#define I2C_RX_NOTIFIED_VALUE   (0x61u)

Definition at line 91 of file i2c.h.

◆ I2C_RXDMAEN

#define I2C_RXDMAEN   (0x1u)

Definition at line 74 of file i2c.h.

◆ I2C_TASK_HANDLE

#define I2C_TASK_HANDLE   (ftsk_taskHandleI2c)

Definition at line 80 of file i2c.h.

◆ I2C_TIMEOUT_us

#define I2C_TIMEOUT_us   (1000u)

Definition at line 69 of file i2c.h.

◆ I2C_TX_NOTIFIED_VALUE

#define I2C_TX_NOTIFIED_VALUE   (0x51u)

Definition at line 89 of file i2c.h.

◆ I2C_TX_TIME_MARGIN_us

#define I2C_TX_TIME_MARGIN_us   (5u)

Definition at line 117 of file i2c.h.

◆ I2C_TXDMAEN

#define I2C_TXDMAEN   (0x2u)

Definition at line 72 of file i2c.h.

Function Documentation

◆ 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_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_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
extern

Definition at line 75 of file i2c.c.

◆ i2c_rxLastByteInterface2

uint8_t i2c_rxLastByteInterface2
extern

Definition at line 77 of file i2c.c.