ZYNQ SDK库函数学习之i2c

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.建立自己的测试程序

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bronceyang131

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值