📄 iic.c
字号:
/*
* File: I2C.c
* Purpose: I2C transfer functions and interrupt handler
*
* Notes:
*
*/
#include "common.h"
#if __cplusplus
extern "C"
{
#endif
void iic_delay(void);
#define nop() asm( nop)
/********************************************************************/
/*
* I2Cinit: I2C initilazation as master
*
* Parameters: None.
*
* Return : None.
*/
void I2Cinit()
{
uint8 temp;
/* Enable the I2C signals */
MCF_GPIO_PASPAR |= ( MCF_GPIO_PASPAR_SDA_SDA
| MCF_GPIO_PASPAR_SCL_SCL);
/* set the frequency near 400KHz, see MCF52223RM table for details */
MCF_I2C_I2FDR = MCF_I2C_I2FDR_IC(0x32);
/* start the module */
MCF_I2C_I2CR = 0 | MCF_I2C_I2CR_IEN;
/* if bit busy set, send a stop condition to slave module */
if( MCF_I2C_I2SR & MCF_I2C_I2SR_IBB)
{
MCF_I2C_I2CR = 0; /* clear control register */
MCF_I2C_I2CR = MCF_I2C_I2CR_IEN | /* enable module */
MCF_I2C_I2CR_MSTA; /* send a START conditionn */
temp = MCF_I2C_I2DR; /* dummy read */
MCF_I2C_I2SR = 0; /* clear status register */
MCF_I2C_I2CR = 0; /* clear control register */
MCF_I2C_I2CR = 0 | MCF_I2C_I2CR_IEN; /* enable the module again */
}
return;
}
/*
* I2CreceiveByte: I2C read byte
*
* Parameters: address: address to read
* id: I2C device to read
*
* Return : data: byte read it from device
*/
uint8 I2CreceiveByte(uint8 address, uint8 id)
{
uint8 data;
MCF_I2C_I2CR |= MCF_I2C_I2CR_MTX; /* setting in Tx mode */
/* send start condition */
MCF_I2C_I2CR |= MCF_I2C_I2CR_MSTA;
MCF_I2C_I2DR = id; /* devide ID to write */
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2DR = address; /* memory address */
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2CR |= MCF_I2C_I2CR_RSTA; /* resend start */
MCF_I2C_I2DR = (uint8)(id | 0x01); /* device id to read */
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MTX; /* setting in Rx mode */
MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK; /* send NO ACK */
data = MCF_I2C_I2DR; /* dummy read */
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
data = MCF_I2C_I2DR; /* read data received */
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
/* generates stop condition */
MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MSTA;
/* send the received data */
return data;
}
/*
* I2CsendByte: send byte to I2C device
*
* Parameters: data: byte to write
* address: address to write
* id: I2C device to write
*
* Return : None.
*/
void I2CsendByte(uint8 data, uint8 address, uint8 id)
{
mcf5xxx_irq_disable();
MCF_I2C_I2CR |= MCF_I2C_I2CR_MTX; /* setting in Tx mode */
/* generates start condition */
MCF_I2C_I2CR |= MCF_I2C_I2CR_MSTA;
MCF_I2C_I2DR = id; /* set devide ID to write */
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2DR = address; /* memory address */
iic_delay();
/* wait until one byte transfer completion */
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
MCF_I2C_I2DR = data; /* memory data */
/* wait until one byte transfer completion */
iic_delay();
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF ))
;
/* clear the completion transfer flag */
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF;
/* generates stop condition */
MCF_I2C_I2CR &= ~MCF_I2C_I2CR_MSTA;
mcf5xxx_irq_enable();
return;
}
/********************************************************************/
void
iic_delay(void)
{
uint16 i;
/* Wait for a bit */
i=500;
while(i--)
nop();
}
void
iic_check(void)
{
uint8 i;
for (i=16;i<23;i++)
{
printf("max3353e reg 0x%02x = 0x%02x\n",i,I2CreceiveByte(i,Slave_Addr));
if (i== 0x11)
i=0x12;
if (i== 0x07)
i=0x0F;
iic_delay();
}
printf("\n");
}
#if __cplusplus
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -