void CI2C1_Interrupt(LDD_RTOS_TISRParameter _isrParameter) { /* {MQXLite RTOS Adapter} ISR parameter is passed as parameter from RTOS interrupt dispatcher */ CI2C1_TDeviceDataPtr DeviceDataPrv = (CI2C1_TDeviceDataPtr)_isrParameter; register uint8_t Status; /* Temporary variable for status register */ Status = I2C_PDD_ReadStatusReg(I2C0_BASE_PTR); /* Safe status register */ I2C_PDD_ClearInterruptFlags(I2C0_BASE_PTR, (Status)); /* Clear interrupt flag */ if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ if (I2C_PDD_GetTransmitMode(I2C0_BASE_PTR) == I2C_PDD_TX_DIRECTION) { /* Is device in Tx mode? */ if ((Status & I2C_PDD_RX_ACKNOWLEDGE) != 0x00U){ /* NACK received? */ I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_SLAVE_MODE); /* Switch device to slave mode (stop signal sent) */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ DeviceDataPrv->OutLenM = 0x00U; /* No character for sending */ DeviceDataPrv->InpLenM = 0x00U; /* No character for reception */ DeviceDataPrv->SerFlag &= (uint8_t)~(MASTER_IN_PROGRES); /* No character for sending or reception */ } else { if (DeviceDataPrv->OutLenM != 0x00U) { /* Is any char. for transmitting? */ DeviceDataPrv->OutLenM--; /* Decrease number of chars for the transmit */ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, *(DeviceDataPrv->OutPtrM)++); /* Send character */ } else { if (DeviceDataPrv->InpLenM != 0x00U) { /* Is any char. for reception? */ if (DeviceDataPrv->InpLenM == 0x01U) { /* If only one char to receive */ I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_DISABLE); /* then transmit ACK disable */ } else { I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_ENABLE); /* else transmit ACK enable */ } I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ (void)I2C_PDD_ReadDataReg(I2C0_BASE_PTR); /* Dummy read character */ } else { DeviceDataPrv->SerFlag &= (uint8_t)~(MASTER_IN_PROGRES); /* Clear flag "busy" */ if (DeviceDataPrv->SendStop == LDD_I2C_SEND_STOP) { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_SLAVE_MODE); /* Switch device to slave mode (stop signal sent) */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ } CI2C1_OnMasterBlockSent(DeviceDataPrv->UserData); /* Invoke OnMasterBlockSent event */ } } } } else { DeviceDataPrv->InpLenM--; /* Decrease number of chars for the receive */ if (DeviceDataPrv->InpLenM != 0x00U) { /* Is any char. for reception? */ if (DeviceDataPrv->InpLenM == 0x01U) { I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_DISABLE); /* Transmit NACK */ } } else { DeviceDataPrv->SerFlag &= (uint8_t)~(MASTER_IN_PROGRES); /* Clear flag "busy" */ I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_SLAVE_MODE); /* If no, switch device to slave mode (stop signal sent) */ I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_ENABLE); /* Transmit ACK */ } *(DeviceDataPrv->InpPtrM)++ = I2C_PDD_ReadDataReg(I2C0_BASE_PTR); /* Receive character */ if (DeviceDataPrv->InpLenM == 0x00U) { /* Is any char. for reception? */ CI2C1_OnMasterBlockReceived(DeviceDataPrv->UserData); /* Invoke OnMasterBlockReceived event */ } } } else { if ((Status & I2C_PDD_ARBIT_LOST) != 0x00U) { /* Arbitration lost? */ DeviceDataPrv->OutLenM = 0x00U; /* Any character is not for sent */ DeviceDataPrv->InpLenM = 0x00U; /* Any character is not for reception */ DeviceDataPrv->SendStop = LDD_I2C_SEND_STOP; /* Set variable for sending stop condition (for master mode) */ DeviceDataPrv->SerFlag &= (uint8_t)~(MASTER_IN_PROGRES); /* Any character is not for sent or reception*/ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ } } }
void i2c_interrupt_process (i2c_buf_t *buf) { /* * this is the i2c interrupt handler, will be called from a wrapper function, * processor failexpert recommended using register for status, not sure if * necessary. assuming device is always in master mode. */ uint8_t status = I2C_S_REG(I2C0_BASE_PTR); uint8_t errormask = 0x00; // clear interrupt flag I2C_S_REG(I2C0_BASE_PTR) = (uint_8)(status | ((I2C_S_IICIF_MASK & I2C_S_ARBL_MASK) & I2C_S_REG(I2C0_BASE_PTR))); if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ if (I2C_PDD_GetTransmitMode(I2C0_BASE_PTR) == I2C_PDD_TX_DIRECTION) { /* Is device in Tx mode? */ if ((status & I2C_PDD_RX_ACKNOWLEDGE) != 0x00U){ /* NACK received? */ I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_SLAVE_MODE); /* Switch device to slave mode (stop signal sent) */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ buf->tx_req = 0x00U; /* No character for sending */ buf->rx_req = 0x00U; /* No character for reception */ buf->flags &= ~(MASTER_IN_PROGRES); /* No character for sending or reception */ buf->flags |= (ADDR_COMPLETE | REP_ADDR_COMPLETE); /* Set the flag */ errormask |= LDD_I2C_MASTER_NACK; /* Set the Master Nack error mask */ } else { if ((buf->flags & ADDR_COMPLETE) != 0x00U) { /* If 10-bit addr has been completed */ if (buf->tx_req != 0x00U) { /* Is any char. for transmitting? */ buf->tx_req--; /* Decrease number of chars for the transmit */ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, buf->tx_buf[buf->tx_idx++]); /* Send character */ } else { if (buf->rx_req != 0x00U) { /* Is any char. for reception? */ if ((buf->flags & REP_ADDR_COMPLETE) != 0x00U) { /* If repeated start and addr tx has been completed for 10-bit mode ?*/ if (buf->rx_req == 0x01U) { /* If only one char to receive */ I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_DISABLE); /* then transmit ACK disable */ } else { I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_ENABLE); /* else transmit ACK enable */ } I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ (void)I2C_PDD_ReadDataReg(I2C0_BASE_PTR); /* Dummy read character */ } else { /* Repeated address has not been completed for 10-bit addressing mode */ I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* Repeat start cycle generated */ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, (uint8_t)(buf->slave_addr_high | 0x01U)); /* Send slave address high byte*/ buf->flags |= REP_ADDR_COMPLETE; } } else { buf->flags &= ~(MASTER_IN_PROGRES); /* Clear flag "busy" */ if (buf->sendstop == TRUE) { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_SLAVE_MODE); /* Switch device to slave mode (stop signal sent) */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ } } } } else { I2C_PDD_WriteDataReg(I2C0_BASE_PTR, buf->slave_addr_low); /* Send second part of the 10-bit addres */ buf->flags |= (ADDR_COMPLETE); /* Address complete */ } } } else { buf->rx_req--; /* Decrease number of chars for the receive */ if (buf->rx_req != 0x00U) { /* Is any char. for reception? */ if (buf->rx_req == 0x01U) { I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_DISABLE); /* Transmit NACK */ } } else { buf->flags &= ~(MASTER_IN_PROGRES); /* Clear flag "busy" */ I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_SLAVE_MODE); /* If no, switch device to slave mode (stop signal sent) */ I2C_PDD_EnableTransmitAcknowledge(I2C0_BASE_PTR, PDD_ENABLE); /* Transmit ACK */ } buf->rx_buf[buf->rx_idx++] = I2C_PDD_ReadDataReg(I2C0_BASE_PTR); /* Receive character */ } } else { if ((status & I2C_PDD_ARBIT_LOST) != 0x00U) { /* Arbitration lost? */ buf->tx_req = 0x00U; /* Any character is not for sent */ buf->rx_req = 0x00U; /* Any character is not for reception */ buf->sendstop = TRUE; /* Set variable for sending stop condition (for master mode) */ buf->flags &= ~(MASTER_IN_PROGRES); /* Any character is not for sent or reception*/ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_RX_DIRECTION); /* Switch to Rx mode */ errormask |= LDD_I2C_ARBIT_LOST; /* Set the ArbitLost error mask */ } } if (errormask != 0x00) { /* Is any error mask set? */ buf->errors |= errormask; /* Update list of error mask value */ // maybe do something here to handle error? } }