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.

163 lines
3.5 KiB

#include "app_i2c0.h"
#define I2C0_INSTANCE MXC_I2C0
sys_cfg_i2c_t sys_i2c_cfg = NULL;
bool I2C0_Master_Initialization(i2c_speed_t Speed)
{
int error;
I2C_Shutdown(I2C0_INSTANCE);
error = I2C_Init(I2C0_INSTANCE, Speed, &sys_i2c_cfg);
I2C_SetTimeout(I2C0_INSTANCE, 100);
NVIC_ClearPendingIRQ(I2C0_IRQn);
NVIC_DisableIRQ(I2C0_IRQn);
NVIC_EnableIRQ(I2C0_IRQn);
return ((error == E_NO_ERROR) ? true : false);
}
#if 0
static i2c_req_t I2C0_Master_Req;
static int32_t I2C0_MasterError;
static bool isRecvComplete;
static uint32_t I2C0_MasterTickCount;
static void I2C_Master_Callback(i2c_req_t *req, int error);
static uint8_t I2C_Master_TxBuff[256];
static uint8_t I2C_Master_RxBuff[256];
void I2C_Master_Initialization(void)
{
int error;
const sys_cfg_i2c_t sys_i2c_master_cfg = NULL; /* No system specific configuration needed. */
I2C_Shutdown(I2C_MASTER_INSTANCE);
error = I2C_Init(I2C_MASTER_INSTANCE, I2C_MASTER_FREQUENCY, &sys_i2c_master_cfg);
I2C_SetTimeout(I2C_MASTER_INSTANCE, 100);
if(I2C_MASTER_INSTANCE == MXC_I2C0)
{
NVIC_ClearPendingIRQ(I2C0_IRQn);
NVIC_DisableIRQ(I2C0_IRQn);
NVIC_EnableIRQ(I2C0_IRQn);
}
else if(I2C_MASTER_INSTANCE == MXC_I2C1)
{
NVIC_ClearPendingIRQ(I2C1_IRQn);
NVIC_DisableIRQ(I2C1_IRQn);
NVIC_EnableIRQ(I2C1_IRQn);
}
}
//Master interrupt handler
void I2C0_IRQHandler(void)
{
I2C_Handler(I2C_MASTER_INSTANCE);
return;
}
static void I2C_Master_Callback(i2c_req_t *req, int error)
{
I2C_MasterError = error;
isRecvComplete = true;
}
int32_t I2C_Write(uint8_t SlaveAddress, uint8_t* pTxBuffer, uint32_t TxLen)
{
int32_t ret;
ret = I2C_MasterWrite(I2C_MASTER_INSTANCE, (SlaveAddress << 1), pTxBuffer, TxLen, 0);
if(ret != TxLen)
{
I2C_Master_Initialization();
ret = E_COMM_ERR;
}
else
{
ret = E_NO_ERROR;
}
return ret;
}
int32_t I2C_Read(uint8_t SlaveAddress, uint8_t ReadRegAddress, uint8_t* pRxBuffer, uint32_t RxLen)
{
int32_t ret;
if(ret = I2C_MasterWrite(I2C_MASTER_INSTANCE, (SlaveAddress << 1), &ReadRegAddress, 1, true) != 1)
{
I2C_Master_Initialization();
return E_COMM_ERR;
}
if(ret = I2C_MasterRead(I2C_MASTER_INSTANCE, (SlaveAddress << 1), &pRxBuffer[0], RxLen, false) != RxLen)
{
I2C_Master_Initialization();
return E_COMM_ERR;
}
return E_NO_ERROR;
}
int32_t I2C_WriteRead(uint8_t SlaveAddress, uint8_t* pWriteBuff, uint32_t TxLen, uint8_t* pRxBuffer, uint32_t RxLen)
{
int32_t ret;
if(ret = I2C_MasterWrite(I2C_MASTER_INSTANCE, (SlaveAddress << 1), &pWriteBuff[0], TxLen, true) != TxLen)
{
I2C_Master_Initialization();
return E_COMM_ERR;
}
if(ret = I2C_MasterRead(I2C_MASTER_INSTANCE, (SlaveAddress << 1), &pRxBuffer[0], RxLen, false) != RxLen)
{
I2C_Master_Initialization();
return E_COMM_ERR;
}
return E_NO_ERROR;
}
void I2C_Scanner_Process(void)
{
uint8_t regAddress = 0x00;
uint8_t regRxdata;
for(uint8_t address = 1; address < 127; address++)
{
dbg_printf(".");
if(I2C_Read(address, regAddress, &regRxdata, 1) == E_NO_ERROR)
{
dbg_printf("\r\nFound slave ID %03d; 0x%02X\r\n", address, address);
//break;
}
Delay_ms(5);
}
dbg_printf("\r\n");
dbg_printf("scan complete\r\n");
}
#endif