This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
void I2CInit(void) { MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2); MAP_GPIOPinConfigure(GPIO_PL0_I2C2SDA); MAP_GPIOPinTypeI2C(GPIO_PORTL_BASE, GPIO_PIN_0); MAP_GPIOPinConfigure(GPIO_PL1_I2C2SCL); MAP_GPIOPinTypeI2CSCL(GPIO_PORTL_BASE, GPIO_PIN_1); I2CMasterInitExpClk(I2C2_BASE, 120000000, 1); I2CMasterEnable(I2C2_BASE); }
/* ********************************************************************************************************* ** 函数名称: FM24CL64WritePolled() ** 函数功能: 向FM24CL64写字符 ** 入口参数: pucdata 数据发送缓冲区指针 ucAddr FM25CL64子地址 ulCount 写入的个数 ** 出口参数 true 写入成功 false 写入失败 ********************************************************************************************************* */ bool FM24CL64Write(uint8_t fm24cl64_addr, uint32_t ucAddr, uint8_t *pucData, uint32_t ulCount) { uint32_t ulToWrite; MAP_I2CMasterSlaveAddrSet(I2C2_BASE, fm24cl64_addr, false); ulToWrite = ulCount; MAP_I2CMasterDataPut(I2C2_BASE, ucAddr/256); MAP_I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_SEND_START); if(!WaitI2CFinished()) { return(false); } MAP_I2CMasterDataPut(I2C2_BASE, ucAddr%256); MAP_I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); if(!WaitI2CFinished()) { return(false); } while(ulToWrite) { MAP_I2CMasterDataPut(I2C2_BASE, *pucData++); if(ulToWrite) { MAP_I2CMasterControl(I2C2_BASE, ((ulToWrite == 1) ? I2C_MASTER_CMD_BURST_SEND_FINISH : I2C_MASTER_CMD_BURST_SEND_CONT)); } if(!WaitI2CFinished()) { return(false); } ulToWrite--; } return(true); }
/* ********************************************************************************************************* ** 函数名称: FM24CL64ReadPolled() ** 函数功能: 向FM24CL64读数据 ** 入口参数: pucdata 数据接收缓冲区指针 ulOffset FM25CL64子地址 ulCount 读出的个数 ** 出口参数 true 读出成功 false 读出失败 ********************************************************************************************************* */ bool FM24CL64Read(uint8_t fm24cl64_addr, uint32_t ulOffset, uint8_t *pucData, uint32_t ulCount) { uint32_t ulToRead; I2CMasterSlaveAddrSet(I2C2_BASE, fm24cl64_addr, false); I2CMasterDataPut(I2C2_BASE, ulOffset/256); I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_SEND_START); if(!WaitI2CFinished()) { return(false); } I2CMasterDataPut(I2C2_BASE, ulOffset%256); I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); if(!WaitI2CFinished()) { return(false); } I2CMasterSlaveAddrSet(I2C2_BASE, fm24cl64_addr, true); I2CMasterControl(I2C2_BASE, ((ulCount > 1) ? I2C_MASTER_CMD_BURST_RECEIVE_START : I2C_MASTER_CMD_SINGLE_RECEIVE)); ulToRead = ulCount; while(ulToRead) { if(!WaitI2CFinished()) { return(false); } *pucData++ = I2CMasterDataGet(I2C2_BASE); ulToRead--; if(ulToRead) { I2CMasterControl(I2C2_BASE, ((ulToRead == 1) ? I2C_MASTER_CMD_BURST_RECEIVE_FINISH : I2C_MASTER_CMD_BURST_RECEIVE_CONT)); } } return(true); }
bool WaitI2CFinished(void) { uint32_t wait_count = 0xffffff; while(MAP_I2CMasterBusy(I2C2_BASE)) { if(wait_count == 0) { return false; } else { wait_count--; } } return true; }
int main() { uint8_t pucReadData[6]={0}; uint8_t pucWriteData[6]={0}; uint32_t index = 0; g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ|SYSCTL_OSC_MAIN| SYSCTL_USE_PLL|SYSCTL_CFG_VCO_480), 120000000); I2CInit(); for(index=0; index<6; index++) { pucWriteData[index] = (0x09+index); } while(1) { SysCtlDelay(120000); FM24CL64Write(0x52, 0x00,pucWriteData, 3); SysCtlDelay(12000); FM24CL64Read(0x52, 0x00, pucReadData,3); } }
读写铁电单步调试FM24CL64Write和FM24CL64Read就可以正常读写,全速就不正确,后来经过各种调试发现在WaitI2CFinished函数里面加 SysCtlDelay(10);的延时就正常了,这到底是什么原因啊?是否存在这种bug?
楼主你好,
从你描述的情况来看,应该是数据发送之后,I2CMCS寄存器的busy位要过一段时间才能清零,能否加大变量wait_count再测试一下?或者直接使用语句
while(I2CMasterBusy(I2C2_MASTER_BASE));看是否会卡在这个while循环中。