hal_i2c.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 498 行 · 第 1/2 页
C
498 行
/* */
/* Function Name : int16_t HalI2c_i2cSlvRxDataEnd(uint8_t *data) */
/* Input : Pointer to receive data */
/* Output : HAL_OK(1) */
/* : HAL_PARAM_ERROR(-2) */
/* */
/* Note : It receives the data. */
/* */
/*****************************************************************************/
int16_t HalI2c_i2cSlvRxDataEnd(uint8_t *data)
{
int16_t rtnVal = HAL_OK;
int16_t sta;
*data = (uint8_t)OkiCLib_read16(I2CDR1); /* get transmit data */
OkiCLib_clr16bit(I2CSR1, I2CSR1_I2CMCF); /* clear I2CSR_I2CMCF */
sta = i2c_Slv_Get_RxD_result();
if(sta == HAL_I2C_MCF){ /* MCF */
rtnVal = HAL_I2C_OTHER_ERROR;
}
i2c_Slv_tramsmit_complete();
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : int16_t HalI2c_i2cMasReRxData(uint8_t *data) */
/* Input : Pointer to receive data */
/* Output : HAL_OK(1) */
/* : HAL_PARAM_ERROR(-2) */
/* */
/* Note : It receives the data. */
/* */
/*****************************************************************************/
int16_t HalI2c_i2cMasReRxData(uint8_t *data)
{
int16_t rtnVal = HAL_OK;
int16_t sta;
OkiCLib_write16(I2CSR0, 0x0000); /* clear all status register */
OkiCLib_write16(I2CDR0, (uint16_t)(i2c_hal_status.slave_addr|READ_FLAG)); /* set serve address & write flag */
OkiCLib_write16(I2CCTL0, (uint16_t)((OkiCLib_read16(I2CCTL0)&0xdfef)|I2CCTL0_I2CRSTA|I2CCTL0_I2CTXAK)); /* tramsmit restart condition & TXAK=1, MTX=0, CS=0 */
sta = i2c_Mas_Get_TxD_result();
if(sta == HAL_I2C_ACK){
OkiCLib_clr16bit(I2CSR0, I2CSR0_I2CMCF); /* clear I2CSR_I2CMCF */
sta = i2c_Mas_Get_TxD_result();
if(sta != HAL_I2C_NO_FREE_BUS){
*data = (uint8_t)OkiCLib_read16(I2CDR0); /* get transmit data */
}else{
rtnVal = HAL_I2C_BUS_ERROR;
}
}else if(sta == HAL_I2C_NO_ACK){
rtnVal = HAL_I2C_ADDR_ACK_ERROR;
}else{
rtnVal = HAL_I2C_BUS_ERROR;
}
OkiCLib_write16(I2CCTL0, (uint16_t)(OkiCLib_read16(I2CCTL0)&0xffd3)); /* tramsmit stop condition & MSTA, RSTA, TXAK=0 */
OkiCLib_write16(I2CSR0, 0x0000); /* clear all status register */
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : int16_t HalI2c_i2cSlvReTxData(uint8_t *data) */
/* Input : Pointer to transmission data */
/* Output : HAL_OK(1) */
/* : HAL_PARAM_ERROR(-2) */
/* */
/* Note : Data is transmitted. */
/* */
/*****************************************************************************/
int16_t HalI2c_i2cSlvReTxData(uint8_t *data)
{
int16_t rtnVal = HAL_OK;
int16_t sta;
if (data == NULL) {
rtnVal = HAL_PARAM_ERROR;
}
if(rtnVal == HAL_OK){
if(((OkiCLib_read16(I2CSR1))&I2CSR1_I2CSRW) == I2CSR1_I2CSRW){ /* check transmission direction */
OkiCLib_write16(I2CDR1, *data); /* set transmit data */
sta = i2c_Slv_Get_TxD_result();
if(sta == HAL_I2C_ACK){ /* ACK */
rtnVal = HAL_I2C_DATA_ACK_ERROR;
}
}else{
rtnVal = HAL_I2C_DIRECT_ERROR;
}
i2c_Slv_tramsmit_complete();
}
return rtnVal;
}
uint16_t i2c_read_register;
/*****************************************************************************/
/* */
/* Function Name : static int16_t i2c_Mas_Get_TxD_result(void) */
/* Input : Nothing */
/* Output : transmit result */
/* : HAL_I2C_ACK */
/* : HAL_I2C_NO_ACK */
/* : HAL_I2C_NO_FREE_BUS */
/* */
/* Note : Get transmit result. */
/* */
/*****************************************************************************/
static int16_t i2c_Mas_Get_TxD_result(void)
{
int16_t rtnVal = HAL_I2C_ACK;
if(((OkiCLib_read16(I2CSR0))&I2CSR0_I2CMAL) != I2CSR0_I2CMAL){ /* check bus error */
do{
i2c_read_register = OkiCLib_read16(I2CSR0);
}while((i2c_read_register&I2CSR0_I2CMCF) != I2CSR0_I2CMCF);/* waiting for transmit complete */
if((i2c_read_register&I2CSR0_I2CRXAK) == I2CSR0_I2CRXAK){ /* check ACK receive */
rtnVal = HAL_I2C_NO_ACK;
}
}else{
rtnVal = HAL_I2C_NO_FREE_BUS;
}
return rtnVal;
}
/*****************************************************************************/
/* */
/* Function Name : static int16_t i2c_Slv_Get_RxD_result(void) */
/* Input : Nothing */
/* Output : transmit result */
/* : HAL_I2C_MCF */
/* : HAL_I2C_STP */
/* */
/* Note : Get transmit result. */
/* */
/*****************************************************************************/
static int16_t i2c_Slv_Get_RxD_result(void)
{
int16_t sta;
do{
i2c_read_register = OkiCLib_read16(I2CSR1);
/* waiting for transmit complete */
}while(((i2c_read_register&I2CSR1_I2CMCF) != I2CSR1_I2CMCF)&&((i2c_read_register&I2CSR1_I2CSTP) != I2CSR1_I2CSTP));
if((i2c_read_register&I2CSR1_I2CMCF) == I2CSR1_I2CMCF){
sta = HAL_I2C_MCF; /* MCF */
}else{
sta = HAL_I2C_STP; /* STP */
}
return sta;
}
/*****************************************************************************/
/* */
/* Function Name : static int16_t i2c_Slv_Get_TxD_result(void) */
/* Input : Nothing */
/* Output : transmit result */
/* : HAL_I2C_ACK */
/* : HAL_I2C_NO_ACK */
/* */
/* Note : Get transmit result. */
/* */
/*****************************************************************************/
static int16_t i2c_Slv_Get_TxD_result(void)
{
int16_t sta = HAL_I2C_ACK;
do{
i2c_read_register = OkiCLib_read16(I2CSR1);
/* waiting for transmit complete */
}while((i2c_read_register&I2CSR1_I2CMCF) != I2CSR1_I2CMCF);
if((i2c_read_register&I2CSR1_I2CRXAK) == I2CSR1_I2CRXAK){
sta = HAL_I2C_NO_ACK;
}
return sta;
}
/*****************************************************************************/
/* */
/* Function Name : void i2c_Mas_tramsmit_stop_condition(void) */
/* Input : Nothing */
/* Output : Nothing */
/* */
/* Note : tramsmit stop condition & clear all status register. */
/* */
/*****************************************************************************/
static void i2c_Mas_tramsmit_stop_condition(void)
{
OkiCLib_clr16bit(I2CCTL0, I2CCTL0_I2CMSTA); /* tramsmit stop condition */
OkiCLib_write16(I2CSR0, 0x0000); /* clear all status register */
}
/*****************************************************************************/
/* */
/* Function Name : void i2c_Slv_tramsmit_complete(void) */
/* Input : Nothing */
/* Output : Nothing */
/* */
/* Note : tramsmit stop condition & clear all status register. */
/* */
/*****************************************************************************/
static void i2c_Slv_tramsmit_complete(void)
{
OkiCLib_write16(I2CSR1, 0x0000); /* clear all status register */
}
/*****************************************************************************/
/* */
/* Function Name : void i2c0_handler(void) */
/* Input : Nothing */
/* Output : Nothing */
/* */
/* Note : I2C c1 handler. */
/* */
/*****************************************************************************/
static void _i2c0_handler(void)
{
uint32_t sta = OkiCLib_read16(I2CSR0);
if((sta&I2CSR0_I2CMAAS) == I2CSR0_I2CMAAS)
{
OkiCLib_clr16bit(I2CSR0, I2CSR0_I2CMAAS); /* clear I2CMAAS flag */
CALL_BACK_TABLE[INT_I2C0](CALLBACK_STATE_I2C_RXD); /* Notice receive completion interruption */
}
}
/*****************************************************************************/
/* */
/* Function Name : void i2c1_handler(void) */
/* Input : Nothing */
/* Output : Nothing */
/* */
/* Note : I2C c1 handler. */
/* */
/*****************************************************************************/
static void _i2c1_handler(void)
{
uint32_t sta = OkiCLib_read16(I2CSR1);
if((sta&I2CSR1_I2CMAAS) == I2CSR1_I2CMAAS)
{
OkiCLib_clr16bit(I2CSR1, I2CSR1_I2CMAAS); /* clear I2CMAAS flag */
CALL_BACK_TABLE[INT_I2C1](CALLBACK_STATE_I2C_RXD); /* Notice receive completion interruption */
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?