📄 bsp_i2c.c
字号:
VICIntEnable = (1 << VIC_I2C0); /* Enable the interrupt source */
OSSemPend(I2C_SemWait, 0, &err); /* Wait until complete */
OSSemPost(I2C_SemBusy); /* Free I2C */
}
/*
*********************************************************************************************************
* I2C_ReadReg()
*
* Description : Read a register on an I2C device.
*
* Argument(s) : p_buf Pointer to the buffer into which the bytes will be stored.
*
* count Number of bytes to read.
*
* reg Register to read.
*
* Return(s) : none.
*********************************************************************************************************
*/
void I2C_ReadReg (CPU_INT08U address,
CPU_INT08U *p_buf,
CPU_INT08U count,
CPU_INT08U reg)
{
CPU_INT08U err;
OSSemPend(I2C_SemBusy, 0, &err); /* Wait until I2C is free */
I20CONCLR = I2CON_I2EN
| I2CON_STA
| I2CON_SI
| I2CON_AA;
I20CONSET = I2CON_I2EN;
I2C_Address = (address & 0xFE); /* Clear lower bit of address */
I2C_BufPtr = ®
I2C_Count = 1;
I20CONSET = I2CON_STA
| I2CON_AA;
VICIntEnable = (1 << VIC_I2C0); /* Enable the interrupt source */
OSSemPend(I2C_SemWait, 0, &err); /* Wait until complete */
I20CONCLR = I2CON_I2EN
| I2CON_STA
| I2CON_SI
| I2CON_AA;
I20CONSET = I2CON_I2EN;
I2C_Address = (address | 0x01); /* Set lower bit of address */
I2C_BufPtr = p_buf;
I2C_Count = count;
I20CONSET = I2CON_STA
| I2CON_AA;
VICIntEnable = (1 << VIC_I2C0); /* Enable the interrupt source */
OSSemPend(I2C_SemWait, 0, &err); /* Wait until complete */
OSSemPost(I2C_SemBusy); /* Free I2C */
}
/*
*********************************************************************************************************
* I2C_Write()
*
* Description : Write to the I2C bus.
*
* Argument(s) : p_buf Pointer to the buffer storing the data to be written.
*
* count Number of bytes to write.
*
* Return(s) : none.
*********************************************************************************************************
*/
void I2C_Write (CPU_INT08U address,CPU_INT08U *p_buf, CPU_INT08U count)
{
CPU_INT08U err;
OSSemPend(I2C_SemBusy, 0, &err); /* Wait until I2C is free */
I20CONCLR = I2CON_I2EN
| I2CON_STA
| I2CON_SI
| I2CON_AA;
I20CONSET = I2CON_I2EN;
I2C_Address = (address & 0xFE); /* Clear lower bit of address */
I2C_BufPtr = p_buf;
I2C_Count = count;
I20CONSET = I2CON_STA
| I2CON_AA;
VICIntEnable = (1 << VIC_I2C0); /* Enable the interrupt source */
OSSemPend(I2C_SemWait, 0, &err); /* Wait until complete */
OSSemPost(I2C_SemBusy); /* Free I2C */
}
/*
*********************************************************************************************************
* I2C_ISR_Handler()
*
* Description : The I2C ISR handler.
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************
*/
void I2C_ISR_Handler (void)
{
CPU_INT32U status;
CPU_INT32U temp;
status = I20STAT;
temp = 0;
switch (status & 0xF8) {
case I2STAT_START:
case I2STAT_REPEATED_START:
I20DAT = I2C_Address;
I20CONCLR = I2CON_STA | I2CON_SI;
break;
case I2STAT_SLA_W_ACK:
temp = *I2C_BufPtr;
I20DAT = temp;
I2C_BufPtr += 1;
I2C_Count -= 1;
I20CONCLR = I2CON_STA | I2CON_SI;
break;
case I2STAT_DAT_T_ACK:
if (I2C_Count > 0) {
temp = *I2C_BufPtr;
I20DAT = temp;
I2C_BufPtr += 1;
I2C_Count -= 1;
I20CONCLR = I2CON_STA | I2CON_SI;
} else {
I20CONCLR = I2CON_STA | I2CON_SI;
I20CONSET = I2CON_STO;
OSSemPost(I2C_SemWait);
VICIntEnClear = (1 << VIC_I2C0);
}
break;
case I2STAT_SLA_W_NOACK:
case I2STAT_DAT_T_NOACK:
case I2STAT_SLA_R_NOACK:
I20CONSET = I2CON_STO;
I20CONCLR = I2CON_STA | I2CON_SI;
OSSemPost(I2C_SemWait);
VICIntEnClear = (1 << VIC_I2C0);
break;
case I2STAT_ARB_LOST:
I20CONCLR = I2CON_STA | I2CON_SI;
OSSemPost(I2C_SemWait);
VICIntEnClear = (1 << VIC_I2C0);
break;
case I2STAT_SLA_R_ACK:
if (I2C_Count <= 1) {
I20CONSET = I2CON_AA;
} else {
I20CONCLR = I2CON_AA;
}
I20CONCLR = I2CON_STA | I2CON_SI;
break;
case I2STAT_DAT_R_ACK:
temp = I20DAT;
*I2C_BufPtr = temp;
I2C_BufPtr += 1;
I2C_Count -= 1;
if (I2C_Count <= 1) {
I20CONCLR = I2CON_AA;
}
I20CONCLR = I2CON_STA | I2CON_SI;
break;
case I2STAT_DAT_R_NOACK:
temp = I20DAT;
*I2C_BufPtr = temp;
I2C_Count -= 1;
I20CONSET = I2CON_STO;
I20CONCLR = I2CON_STA | I2CON_SI;
OSSemPost(I2C_SemWait);
VICIntEnClear = (1 << VIC_I2C0);
break;
default:
I20CONCLR = I2CON_STA | I2CON_SI;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -