此帖出自RF/无线论坛
最新回复
这样是不是代码好看点
1、PinMuxConfig()函数。设置端口功能、PinMuxConfig()函数。设置端口功能
其主要有:
// Enable Peripheral Clocks
MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
// Configure PIN_01 for I2C0 I2C_SCL
MAP_PinTypeI2C(PIN_01, PIN_MODE_1);
// Configure PIN_02 for I2C0 I2C_SDA
MAP_PinTypeI2C(PIN_02, PIN_MODE_1);
2、打开I2C模块
//! Enables and configures the I2C peripheral
int I2C_IF_Open(unsigned long ulMode)
{
// Enable I2C Peripheral
//MAP_HwSemaphoreLock(HWSEM_I2C, HWSEM_WAIT_FOR_EVER);
PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
PRCMPeripheralReset(PRCM_I2CA0);
// Configure I2C module in the specified mode
switch(ulMode)
{
case I2C_MASTER_MODE_STD: /* 100000 */
I2CMasterInitExpClk(I2C_BASE,SYS_CLK,false);
break;
case I2C_MASTER_MODE_FST: /* 400000 */
I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true);
break;
default:
I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true);
break;
}
// Disables the multi-master mode
//MAP_I2CMasterDisable(I2C_BASE);
return SUCCESS;
}
3、I2C读写
int I2C_IF_Read(unsigned char ucDevAddr,
unsigned char *pucData,
unsigned char ucLen)
{
unsigned long ulCmdID;
RETERR_IF_TRUE(pucData == NULL);
RETERR_IF_TRUE(ucLen == 0);
// Set I2C codec slave address
MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, true);
// Check if its a single receive or burst receive
if(ucLen == 1)
{
// Configure for a single receive
ulCmdID = I2C_MASTER_CMD_SINGLE_RECEIVE;
}
else
{
// Initiate a burst receive sequence
ulCmdID = I2C_MASTER_CMD_BURST_RECEIVE_START;
}
// Initiate the transfer.
RET_IF_ERR(I2CTransact(ulCmdID));
// Decrement the count and increment the data pointer
// to facilitate the next transfer
ucLen--;
// Loop until the completion of reception or error
while(ucLen)
{
// Receive the byte over I2C
*pucData = MAP_I2CMasterDataGet(I2C_BASE);
// Decrement the count and increment the data pointer
// to facilitate the next transfer
ucLen--;
pucData++;
if(ucLen)
{
// Continue the reception
RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_CONT));
}
else
{
// Complete the last reception
RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_FINISH));
}
}
// Receive the byte over I2C
*pucData = MAP_I2CMasterDataGet(I2C_BASE);
return SUCCESS;
}
int I2C_IF_Write(unsigned char ucDevAddr,
unsigned char *pucData,
unsigned char ucLen,
unsigned char ucStop)
{
RETERR_IF_TRUE(pucData == NULL);
RETERR_IF_TRUE(ucLen == 0);
// Set I2C codec slave address
MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, false);
// Write the first byte to the controller.
MAP_I2CMasterDataPut(I2C_BASE, *pucData);
// Initiate the transfer.
RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_START));
// Decrement the count and increment the data pointer
// to facilitate the next transfer
ucLen--;
pucData++;
// Loop until the completion of transfer or error
while(ucLen)
{
// Write the next byte of data
MAP_I2CMasterDataPut(I2C_BASE, *pucData);
// Transact over I2C to send byte
RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_CONT));
// Decrement the count and increment the data pointer
// to facilitate the next transfer
ucLen--;
pucData++;
}
// If stop bit is to be sent, send it.
if(ucStop == true)
{
RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_STOP));
}
return SUCCESS;
}
4、关闭I2C
int I2C_IF_Close()
{
//
// Power OFF the I2C peripheral
//
MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
return SUCCESS;
}
复制代码
详情
回复
发表于 2016-10-2 11:44
| ||
|
||
此帖出自RF/无线论坛
| ||
|
||
EEWorld Datasheet 技术支持