poll模式:
主要函數分析
1.慣例的初始化函數
XIicPs_LookupConfig(DeviceId); //ID ,基地址,時鐘
XIicPs_CfgInitialize(I2C_Ptr, Config, Config->BaseAddress);
2.自檢函數,直接使用
/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XIicPs_SelfTest(I2C_Ptr);
3.設置時鐘函數
/*
* Set the IIC serial clock rate.
*/
XIicPs_SetSClk(I2C_Ptr, IIC_SCLK_RATE);
4.加以封裝的寫函數
void I2cPs_write(XIicPs *I2C_Ptr, u8 *MsgPtr, s32 ByteCount, u16 SlaveAddr)
{
XIicPs_MasterSendPolled(I2C_Ptr, MsgPtr, ByteCount, SlaveAddr);
while (XIicPs_BusIsBusy(I2C_Ptr)) {
}
usleep(2000);
}
XIicPs_MasterSendPolled(XIicPs *InstancePtr, u8 *MsgPtr,s32 ByteCount, u16 SlaveAddr)
在master模式下采用poll方式發送數據,參數分爲:指針實例,發送數據的首地址,發送字節數,從器件地址。不做分析,直接使用。
XIicPs_BusIsBusy(XIicPs *InstancePtr) 總線是否忙查詢函數,busy的時候要延時
@return
* - TRUE if the bus is busy.
* - FALSE if the bus is not busy.
5.已經封裝的讀函數
void I2cPs_read(XIicPs *I2C_Ptr, u8 *MsgPtr, s32 ByteCount, u16 SlaveAddr)
{
XIicPs_MasterRecvPolled(I2C_Ptr, MsgPtr, ByteCount, SlaveAddr);
while (XIicPs_BusIsBusy(I2C_Ptr)) {
}
usleep(2000);
}
XIicPs_MasterRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr,s32 ByteCount, u16 SlaveAddr) 與發送時的函數對應,同樣要做總線是否忙做判斷。
6.對eeprom的讀寫函數
void read_eeprom(u8 addr,u8 len)
{
eeprom_wbuf[0]=addr;
I2cPs_write(&Iic,eeprom_wbuf, 1, EEPROM_ADDR);
I2cPs_read (&Iic,eeprom_rbuf, len, EEPROM_ADDR);
}
void write_eeprom(u8 addr,u8 len)
{
eeprom_wbuf[0]=addr;
I2cPs_write(&Iic,eeprom_wbuf,len+1,EEPROM_ADDR);
}
中斷模式:
再次注意一下中斷方式的幾個函數:
1.自己寫得初始化函數要包括這幾方面:
查找設備的ID,基地址,時鐘,進行初始化,(I2C自檢,設置時鐘頻率),設置中斷處理函數。
2.系統中斷初始化,這個函數是固定,包含兩個方面
void Setup_Intr_Exception(XScuGic * IntcInstancePtr)
{
/* Enable interrupts from the hardware */
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
(void *)IntcInstancePtr);
Xil_ExceptionEnable();
}
int Init_Intr_System(XScuGic * IntcInstancePtr)
{
int Status;
XScuGic_Config *IntcConfig;
/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
if (NULL == IntcConfig) {
return XST_FAILURE;
}
Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
IntcConfig->CpuBaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
return XST_SUCCESS;
}
3.建立中斷連接函數,中斷使能,幾乎也是固定的,各個設備的使能方式不一樣
主要是設備中斷的中斷號,最後使能中斷
int I2cPs_Setup_IntrSystem(XScuGic * GicInstancePtr , XIicPs *I2C_Ptr ,u16 I2cIntrId)
{
int Status;
Status = XScuGic_Connect(GicInstancePtr, I2cIntrId,
(Xil_InterruptHandler)XIicPs_MasterInterruptHandler,
(void *)I2C_Ptr);
if (Status != XST_SUCCESS) {
return Status;
}
XScuGic_Enable(GicInstancePtr, I2cIntrId);
return XST_SUCCESS;
}
4.建立自己的測試程序