feat: KT mBMS 펌웨어 3종(HW1/2/3) 및 통합 빌드 환경 초기 커밋 (V4.0.0.1)
This commit is contained in:
217
APP_R3221/App/Hardware/i2c.c
Normal file
217
APP_R3221/App/Hardware/i2c.c
Normal file
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file i2c.c
|
||||
* @author AMG Energy Solution Team
|
||||
* @version V1.0.0
|
||||
* @date 07-Feb-2022
|
||||
* @brief This file provides all the codes of i2c functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
||||
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
||||
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
||||
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
||||
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2022 AMG</center></h2>
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup mBMS2
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "i2c.h"
|
||||
#include "delay.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Define I2C unit used for the example */
|
||||
#define I2C_UNIT (M4_I2C1)
|
||||
|
||||
/* Define BQ Series device address */
|
||||
#define BQ_ADDRESS (0x50u)
|
||||
#define BQ_ADR_LEN (1u)
|
||||
|
||||
/* Define port and pin for SDA and SCL */
|
||||
#define I2C_SCL_PORT (PortD)
|
||||
#define I2C_SCL_PIN (Pin01)
|
||||
|
||||
#define I2C_SDA_PORT (PortD)
|
||||
#define I2C_SDA_PIN (Pin00)
|
||||
|
||||
#define I2C_GPIO_SCL_FUNC (Func_I2c1_Scl)
|
||||
#define I2C_GPIO_SDA_FUNC (Func_I2c1_Sda)
|
||||
|
||||
#define I2C_FCG_USE (PWC_FCG1_PERIPH_I2C1)
|
||||
|
||||
#define TIMEOUT (0x40000ul)
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
** \brief I2C memory write
|
||||
**
|
||||
** \param u8DevAddr The slave address
|
||||
** \param u8Addr The register address
|
||||
** \param pu8Data Pointer to the data buffer
|
||||
** \param u32Size Data size
|
||||
** \param u32TimeOut Time out count
|
||||
** \retval en_result_t Enumeration value:
|
||||
** - Ok: Success
|
||||
** - Error: Receive NACK
|
||||
** - ErrorTimeout: Time out
|
||||
******************************************************************************/
|
||||
en_result_t I2C_Reg_Write(uint16_t u8DevAddr, uint8_t u8RegAddr, uint8_t *pu8Data, uint32_t u32Size, uint32_t u32TimeOut)
|
||||
{
|
||||
en_result_t enRet;
|
||||
|
||||
I2C_Cmd(I2C_UNIT, Enable);
|
||||
|
||||
I2C_SoftwareResetCmd(I2C_UNIT, Enable);
|
||||
I2C_SoftwareResetCmd(I2C_UNIT, Disable);
|
||||
enRet = I2C_Start(I2C_UNIT, u32TimeOut);
|
||||
if (Ok == enRet)
|
||||
{
|
||||
enRet = I2C_TransAddr(I2C_UNIT, u8DevAddr, I2CDirTrans, u32TimeOut);
|
||||
|
||||
if (Ok == enRet)
|
||||
{
|
||||
enRet = I2C_TransData(I2C_UNIT, (uint8_t *)&u8RegAddr, BQ_ADR_LEN, u32TimeOut);
|
||||
if (Ok == enRet)
|
||||
{
|
||||
enRet = I2C_TransData(I2C_UNIT, pu8Data, u32Size, u32TimeOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
I2C_Stop(I2C_UNIT, u32TimeOut);
|
||||
I2C_Cmd(I2C_UNIT, Disable);
|
||||
|
||||
return enRet;
|
||||
}
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
** \brief I2C memory read
|
||||
**
|
||||
** \param u8DevAddr The slave address
|
||||
** \param u8Addr The register address
|
||||
** \param pu8Data Pointer to the data buffer
|
||||
** \param u32Size Data size
|
||||
** \param u32TimeOut Time out count
|
||||
** \retval en_result_t Enumeration value:
|
||||
** - Ok: Success
|
||||
** - ErrorTimeout: Time out
|
||||
******************************************************************************/
|
||||
en_result_t I2C_Reg_Read(uint8_t u8DevAddr, uint8_t u8RegAddr, uint8_t *pu8Data, uint32_t u32Size, uint32_t u32TimeOut)
|
||||
{
|
||||
en_result_t enRet;
|
||||
|
||||
I2C_Cmd(I2C_UNIT, Enable);
|
||||
|
||||
I2C_SoftwareResetCmd(I2C_UNIT, Enable);
|
||||
I2C_SoftwareResetCmd(I2C_UNIT, Disable);
|
||||
enRet = I2C_Start(I2C_UNIT, u32TimeOut);
|
||||
if (Ok == enRet)
|
||||
{
|
||||
enRet = I2C_TransAddr(I2C_UNIT, (uint8_t)u8DevAddr, I2CDirTrans, u32TimeOut);
|
||||
|
||||
if (Ok == enRet)
|
||||
{
|
||||
enRet = I2C_TransData(I2C_UNIT, (uint8_t *)&u8RegAddr, BQ_ADR_LEN, u32TimeOut);
|
||||
if(Ok == enRet)
|
||||
{
|
||||
enRet = I2C_Restart(I2C_UNIT, u32TimeOut);
|
||||
if (Ok == enRet)
|
||||
{
|
||||
if (1ul == u32Size)
|
||||
{
|
||||
I2C_AckConfig(I2C_UNIT, I2c_NACK);
|
||||
}
|
||||
|
||||
enRet = I2C_TransAddr(I2C_UNIT, (uint8_t)u8DevAddr, I2CDirReceive, u32TimeOut);
|
||||
if (Ok == enRet)
|
||||
{
|
||||
enRet = I2C_MasterDataReceiveAndStop(I2C_UNIT, pu8Data, u32Size, u32TimeOut);
|
||||
}
|
||||
|
||||
I2C_AckConfig(I2C_UNIT, I2c_ACK);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Ok != enRet)
|
||||
{
|
||||
I2C_Stop(I2C_UNIT, u32TimeOut);
|
||||
}
|
||||
|
||||
I2C_Cmd(I2C_UNIT, Disable);
|
||||
return enRet;
|
||||
}
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
** \brief Initialize the I2C peripheral for e2prom
|
||||
** \param None
|
||||
** \retval en_result_t Enumeration value:
|
||||
** - Ok: Success
|
||||
** - ErrorTimeout: Time out
|
||||
******************************************************************************/
|
||||
static en_result_t E2_Initialize(void)
|
||||
{
|
||||
stc_i2c_init_t stcI2cInit;
|
||||
en_result_t enRet;
|
||||
float32_t fErr;
|
||||
|
||||
I2C_DeInit(I2C_UNIT);
|
||||
|
||||
/* Initialize I2C port*/
|
||||
PORT_SetFunc(I2C_SCL_PORT, I2C_SCL_PIN, I2C_GPIO_SCL_FUNC, Disable);
|
||||
PORT_SetFunc(I2C_SDA_PORT, I2C_SDA_PIN, I2C_GPIO_SDA_FUNC, Disable);
|
||||
|
||||
/* Enable I2C Peripheral*/
|
||||
PWC_Fcg1PeriphClockCmd(I2C_FCG_USE, Enable);
|
||||
|
||||
MEM_ZERO_STRUCT(stcI2cInit);
|
||||
stcI2cInit.u32ClockDiv = I2C_CLK_DIV8;
|
||||
stcI2cInit.u32Baudrate = 100000ul;
|
||||
stcI2cInit.u32SclTime = 0ul;
|
||||
|
||||
enRet = I2C_Init(I2C_UNIT, &stcI2cInit, &fErr);
|
||||
|
||||
I2C_BusWaitCmd(I2C_UNIT, Enable);
|
||||
|
||||
return enRet;
|
||||
}
|
||||
|
||||
void I2c_Config(void)
|
||||
{
|
||||
stc_port_init_t stcPortInit;
|
||||
|
||||
/* configuration structure initialization */
|
||||
MEM_ZERO_STRUCT(stcPortInit);
|
||||
|
||||
stcPortInit.enPinMode = Pin_Mode_Out;
|
||||
stcPortInit.enExInt = Disable;
|
||||
stcPortInit.enPullUp = Disable;
|
||||
|
||||
/* SPI Port/Pin initialization */
|
||||
PORT_Init(I2C_SCL_PORT, I2C_SCL_PIN, &stcPortInit);
|
||||
|
||||
stcPortInit.enPinMode = Pin_Mode_In;
|
||||
|
||||
PORT_Init(I2C_SDA_PORT, I2C_SDA_PIN, &stcPortInit);
|
||||
|
||||
E2_Initialize();
|
||||
}
|
||||
|
||||
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/
|
||||
Reference in New Issue
Block a user