/*---------------------------------------------------------------------------------------------------------*/ void I2C0_IRQHandler(void) { uint32_t u32Status; /* Check I2C Wake-up interrupt flag set or not */ if(I2C_GET_WAKEUP_FLAG(I2C0)) { /* Clear I2C Wake-up interrupt flag */ I2C_CLEAR_WAKEUP_FLAG(I2C0); g_u8SlvI2CWK = 1; return; } u32Status = I2C_GET_STATUS(I2C0); if(I2C_GET_TIMEOUT_FLAG(I2C0)) { /* Clear I2C0 Timeout Flag */ I2C_ClearTimeoutFlag(I2C0); } else { if(s_I2C0HandlerFn != NULL) s_I2C0HandlerFn(u32Status); } }
uint8_t TwoWire::endTransmission(uint8_t sendStop) { int sent = 0; /* Send data */ int timeout_=TIMEOUT; while(timeout_--) { /* Send start */ I2C_START(i2c); I2C_WAIT_READY(i2c); /* Send control byte */ I2C_SET_DATA(i2c, txAddress); I2C_SET_CONTROL_REG(i2c, I2C_SI); I2C_WAIT_READY(i2c); if(I2C_GET_STATUS(i2c)!=0x18) { I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); continue; } sent = 0; while (sent < txBufferLength) { I2C_SET_DATA(i2c, txBuffer[sent++]); I2C_SET_CONTROL_REG(i2c, I2C_SI); I2C_WAIT_READY(i2c); if(I2C_GET_STATUS(i2c)!=0x28) { I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); continue; } } if(sendStop==true) { /* Send stop */ I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); } break; } // empty buffer txBufferLength = 0; status = MASTER_IDLE; return sent; }
/************************************************************************* Issues a repeated start condition and sends address and transfer direction Input: address and transfer direction of I2C device Return: 0 device accessible 1 failed to access device *************************************************************************/ unsigned char lib_i2c_rep_start(unsigned char address) { /* Send data */ I2C_SET_CONTROL_REG(I2C, I2C_STA | I2C_SI); if (!I2C_WAIT_READY_ERROR(I2C)) return 4; if(I2C_GET_STATUS(I2C) != 0x10) // Master repeat start return 3; // transmit address byte + direction if (lib_i2c_write(address)) return 2; return 0; }
/*---------------------------------------------------------------------------------------------------------*/ void I2C3_IRQHandler(void) { uint32_t u32Status; u32Status = I2C_GET_STATUS(I2C3); if (I2C_GET_TIMEOUT_FLAG(I2C3)) { /* Clear I2C3 Timeout Flag */ I2C_ClearTimeoutFlag(I2C3); } else { if (s_I2C3HandlerFn != NULL) s_I2C3HandlerFn(u32Status); } }
/************************************************************************* Send one byte to I2C device Input: byte to be transfered Return: 0 write successful 1 write failed *************************************************************************/ unsigned char lib_i2c_write(unsigned char data) { I2C_SET_DATA(I2C, data); I2C_SET_CONTROL_REG(I2C, I2C_SI); if (!I2C_WAIT_READY_ERROR(I2C)) return 2; // Check ACK uint8_t status = I2C_GET_STATUS(I2C); // Master TX/RX Address/Data ACKs if (status != 0x18 && status != 0x28 && status != 0x40 && status != 0x50) return 1; return 0; }
void TwoWire::onService(void) { uint32_t u32Status; u32Status = I2C_GET_STATUS(i2c); if(I2C_GET_TIMEOUT_FLAG(i2c)) { /* Clear I2C0 Timeout Flag */ I2C_ClearTimeoutFlag(i2c); } else { I2C_SlaveTRx(u32Status); } }
void I2C0_IRQHandler(void) { uint32_t u32Status; u32Status = I2C_GET_STATUS(I2C0); #if 0 //def DEBUG_ENABLE printf("Status 0x%x \n", u32Status); #endif if (I2C_GET_TIMEOUT_FLAG(I2C0)) { /* Clear I2C0 Timeout Flag */ I2C_ClearTimeoutFlag(I2C0); } else { if (s_I2C0HandlerFn != NULL) s_I2C0HandlerFn(u32Status); } }
/*---------------------------------------------------------------------------------------------------------*/ void I2C1_IRQHandler(void) { uint32_t u32Status; // clear interrupt flag I2C1->INTSTS |= I2C_INTSTS_INTSTS_Msk; u32Status = I2C_GET_STATUS(I2C1); if (I2C_GET_TIMEOUT_FLAG(I2C1)) { /* Clear I2C1 Timeout Flag */ I2C_ClearTimeoutFlag(I2C1); } else { if (s_I2C1HandlerFn != NULL) s_I2C1HandlerFn(u32Status); } }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { if (quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH; int readed = 0; int timeout_=TIMEOUT; while(timeout_--) { /* Send start */ I2C_START(i2c); I2C_WAIT_READY(i2c); /* Send control byte */ I2C_SET_DATA(i2c, (address<<1)+1); I2C_SET_CONTROL_REG(i2c, I2C_SI | I2C_AA); I2C_WAIT_READY(i2c); if(I2C_GET_STATUS(i2c)!=0x40) { /* Send stop */ I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); continue; } readed = 0; while (readed < quantity){ /* Read data */ if((readed+1)==quantity) I2C_SET_CONTROL_REG(i2c, I2C_SI); else I2C_SET_CONTROL_REG(i2c, I2C_SI | I2C_AA); I2C_WAIT_READY(i2c); rxBuffer[readed++] = I2C_GET_DATA(i2c); }; if(sendStop==true){ /* Send stop */ I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); }else I2C_SET_CONTROL_REG(i2c, I2C_SI); break; } // set rx buffer iterator vars rxBufferIndex = 0; rxBufferLength = readed; return readed; }