You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.6 KiB

#include "app_i2c_master.h"
static mxc_i2c_regs_t* pI2C_Master = NULL;
static sys_cfg_i2c_t sys_i2c_master_cfg = NULL; /* No system specific configuration needed. */
static void I2C_Master_Handler(void);
bool I2C_Master_Initialization(mxc_i2c_regs_t* pI2C_Handler, i2c_speed_t Speed)
{
int error;
if(!(pI2C_Handler == MXC_I2C0 || pI2C_Handler == MXC_I2C1))
return false;
pI2C_Master = pI2C_Handler;
I2C_Shutdown(pI2C_Master);
3 months ago
error = I2C_Init(pI2C_Master, Speed, &sys_i2c_master_cfg);
3 months ago
if(error != E_NO_ERROR)
return false;
I2C_SetTimeout(pI2C_Master, 100);
if(pI2C_Handler == MXC_I2C0)
{
NVIC_ClearPendingIRQ(I2C0_IRQn);
NVIC_DisableIRQ(I2C0_IRQn);
NVIC_SetVector(I2C0_IRQn, I2C_Master_Handler);
NVIC_EnableIRQ(I2C0_IRQn);
}
else if(pI2C_Handler == MXC_I2C1)
{
NVIC_ClearPendingIRQ(I2C1_IRQn);
NVIC_DisableIRQ(I2C1_IRQn);
NVIC_SetVector(I2C1_IRQn, I2C_Master_Handler);
NVIC_EnableIRQ(I2C1_IRQn);
}
return true;
}
static void I2C_Master_Handler(void)
{
3 months ago
I2C_Handler(pI2C_Master);
}
int32_t I2C_Master_Write(uint8_t SlaveAddress, uint8_t* pTxBuffer, uint32_t TxLen)
{
int32_t ret;
if(pI2C_Master == NULL)
return E_NULL_PTR;
ret = I2C_MasterWrite(pI2C_Master, (SlaveAddress << 1), pTxBuffer, TxLen, 0);
if(ret != TxLen)
{
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
ret = E_COMM_ERR;
}
else
{
ret = E_NO_ERROR;
}
return ret;
}
int32_t I2C_Master_Read(uint8_t SlaveAddress, uint8_t* pRxBuffer, uint32_t RxLen)
{
int32_t ret;
if(pI2C_Master == NULL)
return E_NULL_PTR;
3 months ago
if((ret = I2C_MasterRead(pI2C_Master, (SlaveAddress << 1), &pRxBuffer[0], RxLen, false)) != RxLen)
{
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
return E_COMM_ERR;
}
return E_NO_ERROR;
}
int32_t I2C_Master_WriteRead(uint8_t SlaveAddress, uint8_t* pWriteBuff, uint32_t TxLen, uint8_t* pRxBuffer, uint32_t RxLen)
{
int32_t ret;
if(pI2C_Master == NULL)
return E_NULL_PTR;
3 months ago
if((ret = I2C_MasterWrite(pI2C_Master, (SlaveAddress << 1), &pWriteBuff[0], TxLen, true)) != TxLen)
{
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
return E_COMM_ERR;
}
3 months ago
if((ret = I2C_MasterRead(pI2C_Master, (SlaveAddress << 1), &pRxBuffer[0], RxLen, false)) != RxLen)
{
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
return E_COMM_ERR;
}
return E_NO_ERROR;
}