uint_8 i2c_master_send_block (i2c_buf_t *buf, uint_8 *ptr, uint_32 length) { /* * this function sends a block of data out of the i2c port as master, it also ensures that a * stop condition is sent at the end */ if (!length) { return(E_OK); // nothing to do } if (length > I2C_BUF_MAX) { return(E_BOUNDS); } if ((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY) || /* Is the bus busy? */ \ ((buf->flags & MASTER_IN_PROGRES) != 0x00U) || (buf->tx_req != 0x00U)) { return(E_BUSY); } // // begin critical section // buf->flags |= MASTER_IN_PROGRES; /* Set flag "busy" */ memcpy(buf->tx_buf, ptr, length); buf->rx_idx = 0; buf->tx_idx = 0; buf->rx_req = 0; buf->tx_req = length; /* Set the counter of output bufer's content */ buf->sendstop = TRUE; /* Set generating stop condition */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_TX_DIRECTION); /* Set TX mode */ if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* If yes then repeat start cycle generated */ } else { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_MASTER_MODE); /* If no then start signal generated */ } if ((buf->flags & ADDR_7) != 0x00) { /* Is 7-bit addressing set ? */ buf->flags |= (ADDR_COMPLETE | REP_ADDR_COMPLETE); /* Only one byte of address will be sent 7-bit address mode*/ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, buf->slave_addr_low); /* Send slave address */ } else if ((buf->flags & ADDR_10) != 0x00) { /* Is 10-bit addressing set ? */ buf->flags &= ~(ADDR_COMPLETE | REP_ADDR_COMPLETE); /* Second byte of address will be sent later */ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, buf->slave_addr_high); /* Send slave address - high byte*/ } else if ((buf->flags & GENERAL_CALL) != 0x00) { /* Is general call command required ? */ buf->flags |= ADDR_COMPLETE; /* Only one byte of address will be sent in general call address mode*/ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, 0x00); /* Send general call address */ } // // end critical section // return(E_OK); }
/* ===================================================================*/ LDD_TError IntI2cLdd1_MasterReceiveBlock(LDD_TDeviceData *DeviceDataPtr, LDD_TData *BufferPtr, LDD_I2C_TSize Size, LDD_I2C_TSendStop SendStop) { IntI2cLdd1_TDeviceData *DeviceDataPrv = (IntI2cLdd1_TDeviceData *)DeviceDataPtr; if (Size == 0x00U) { /* Test variable Size on zero */ return ERR_OK; /* If zero then OK */ } if (SendStop == LDD_I2C_NO_SEND_STOP) { /* Test variable SendStop on supported value */ return ERR_PARAM_MODE; /* If not supported value then error */ } if ((DeviceDataPrv->SerFlag & GENERAL_CALL) != 0x00U) { /* Is the general call flag set (SelectSlaveDevice - address type is general call) ? */ return ERR_NOTAVAIL; /* It is not possible to receive data - Call SelectSlaveDevice method */ } if (DeviceDataPrv->SendStop == LDD_I2C_SEND_STOP) { if ((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY) || /* Is the bus busy? */ \ ((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || \ (DeviceDataPrv->InpLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } else { if(((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || /* Is the bus busy? */ \ (DeviceDataPrv->InpLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } /* {Default RTOS Adapter} Critical section begin, general PE function is used */ EnterCritical(); DeviceDataPrv->SerFlag |= MASTER_IN_PROGRES; /* Set flag "busy" */ DeviceDataPrv->InpPtrM = (uint8_t *)BufferPtr; /* Save pointer to data for reception */ DeviceDataPrv->InpByteMNum = 0x00U; /* Clear data number */ DeviceDataPrv->InpLenM = Size; /* Set the counter of input bufer's content */ DeviceDataPrv->SendStop = SendStop; /* Set generating stop condition */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_TX_DIRECTION); /* Set TX mode */ if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* If yes then repeat start cycle generated */ } else { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_MASTER_MODE); /* If no then start signal generated */ } if ((DeviceDataPrv->SerFlag & ADDR_7) != 0x00U) { /* Is 7-bit addressing set ? */ DeviceDataPrv->SerFlag |= (ADDR_COMPLETE|REP_ADDR_COMPLETE); /* Only one byte of address will be sent 7-bit address mode*/ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, (uint8_t)(DeviceDataPrv->SlaveAddr | 0x01U)); /* Send slave address */ } else { if ((DeviceDataPrv->SerFlag & ADDR_10) != 0x00U) { /* Is 10-bit addressing set ? */ DeviceDataPrv->SerFlag &= (uint8_t)~(ADDR_COMPLETE | REP_ADDR_COMPLETE); /* Second byte of address will be sent later */ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, DeviceDataPrv->SlaveAddrHigh); /* Send slave address - high byte */ } } /* {Default RTOS Adapter} Critical section end, general PE function is used */ ExitCritical(); return ERR_OK; /* OK */ }
/* ===================================================================*/ LDD_TError IntI2cLdd1_MasterSendBlock(LDD_TDeviceData *DeviceDataPtr, LDD_TData *BufferPtr, LDD_I2C_TSize Size, LDD_I2C_TSendStop SendStop) { IntI2cLdd1_TDeviceData *DeviceDataPrv = (IntI2cLdd1_TDeviceData *)DeviceDataPtr; if (Size == 0x00U) { /* Test variable Size on zero */ return ERR_OK; /* If zero then OK */ } if (DeviceDataPrv->SendStop == LDD_I2C_SEND_STOP) { if ((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY) || /* Is the bus busy? */ \ ((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || \ (DeviceDataPrv->OutLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } else { if (((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || /* Is the bus busy? */ \ (DeviceDataPrv->OutLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } /* {Default RTOS Adapter} Critical section begin, general PE function is used */ EnterCritical(); DeviceDataPrv->SerFlag |= MASTER_IN_PROGRES; /* Set flag "busy" */ DeviceDataPrv->OutPtrM = (uint8_t *)BufferPtr; /* Save pointer to data for transmitting */ DeviceDataPrv->OutByteMNum = 0x00U; /* Set data counter */ DeviceDataPrv->OutLenM = Size; /* Set the counter of output bufer's content */ DeviceDataPrv->SendStop = SendStop; /* Set generating stop condition */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_TX_DIRECTION); /* Set TX mode */ if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* If yes then repeat start cycle generated */ } else { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_MASTER_MODE); /* If no then start signal generated */ } if ((DeviceDataPrv->SerFlag & ADDR_7) != 0x00U) { /* Is 7-bit addressing set ? */ DeviceDataPrv->SerFlag |= (ADDR_COMPLETE | REP_ADDR_COMPLETE); /* Only one byte of address will be sent 7-bit address mode*/ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, DeviceDataPrv->SlaveAddr); /* Send slave address */ } else { if ((DeviceDataPrv->SerFlag & ADDR_10) != 0x00U) { /* Is 10-bit addressing set ? */ DeviceDataPrv->SerFlag &= (uint8_t)~(ADDR_COMPLETE | REP_ADDR_COMPLETE); /* Second byte of address will be sent later */ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, DeviceDataPrv->SlaveAddrHigh); /* Send slave address - high byte */ } else { if ((DeviceDataPrv->SerFlag & GENERAL_CALL) != 0x00U) { /* Is general call command required ? */ DeviceDataPrv->SerFlag |= ADDR_COMPLETE; /* Only one byte of address will be sent in general call address mode*/ I2C_PDD_WriteDataReg(I2C0_BASE_PTR, 0x00U); /* Send general call address */ } } } /* {Default RTOS Adapter} Critical section end, general PE function is used */ ExitCritical(); return ERR_OK; /* OK */ }
/* ** =================================================================== ** Method : Accel_SendStop (component InternalI2C) ** Description : ** If the "Automatic stop condition" property value is 'no', ** this method sends a valid stop condition to the serial data ** line of the I2C bus to terminate the communication on the ** bus after using send methods. This method is enabled only if ** "Automatic stop condition" property is set to 'no'. ** Parameters : None ** Returns : ** --- - Error code, possible codes: ** ERR_OK - OK ** ERR_SPEED - This device does not work in ** the active speed mode ** ERR_DISABLED - Device is disabled ** ERR_BUSOFF - Clock timeout elapsed - bus is ** busy ** =================================================================== */ byte Accel_SendStop(void) { LDD_I2C_TBusState BusState; word Tr; (void)Accel_SerFlag; /* Suppress unused variable warning if needed */ 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 */ for(Tr=0x2710U; Tr!=0U; Tr--) { (void)IntI2cLdd1_CheckBus(IntI2cLdd1_DeviceDataPtr, &BusState); /* Get I2C line state */ if(BusState == LDD_I2C_IDLE) { /* Bus is idle? */ return ERR_OK; /* Return without error */ } } return ERR_BUSOFF; /* Return with error */ }
/* ===================================================================*/ LDD_TError I2C2_MasterReceiveBlock(LDD_TDeviceData *DeviceDataPtr, LDD_TData *BufferPtr, LDD_I2C_TSize Size, LDD_I2C_TSendStop SendStop) { I2C2_TDeviceData *DeviceDataPrv = (I2C2_TDeviceData *)DeviceDataPtr; if (Size == 0x00U) { /* Test variable Size on zero */ return ERR_OK; /* If zero then OK */ } if (SendStop == LDD_I2C_NO_SEND_STOP) { /* Test variable SendStop on supported value */ return ERR_PARAM_MODE; /* If not supported value then error */ } if (DeviceDataPrv->SendStop == LDD_I2C_SEND_STOP) { if ((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY) || /* Is the bus busy? */ \ ((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || \ (DeviceDataPrv->InpLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } else { if(((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || /* Is the bus busy? */ \ (DeviceDataPrv->InpLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } /* {Default RTOS Adapter} Critical section begin, general PE function is used */ EnterCritical(); DeviceDataPrv->SerFlag |= MASTER_IN_PROGRES; /* Set flag "busy" */ DeviceDataPrv->InpPtrM = (uint8_t *)BufferPtr; /* Save pointer to data for reception */ DeviceDataPrv->InpLenM = Size; /* Set the counter of input bufer's content */ DeviceDataPrv->SendStop = SendStop; /* Set generating stop condition */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_TX_DIRECTION); /* Set TX mode */ if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* If yes then repeat start cycle generated */ } else { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_MASTER_MODE); /* If no then start signal generated */ } I2C_PDD_WriteDataReg(I2C0_BASE_PTR, 0x3BU); /* Send slave address */ /* {Default RTOS Adapter} Critical section end, general PE function is used */ ExitCritical(); return ERR_OK; /* OK */ }
/* ===================================================================*/ LDD_TError CI2C1_MasterSendBlock(LDD_TDeviceData *DeviceDataPtr, LDD_TData *BufferPtr, LDD_I2C_TSize Size, LDD_I2C_TSendStop SendStop) { CI2C1_TDeviceData *DeviceDataPrv = (CI2C1_TDeviceData *)DeviceDataPtr; if (Size == 0x00U) { /* Test variable Size on zero */ return ERR_OK; /* If zero then OK */ } if (DeviceDataPrv->SendStop == LDD_I2C_SEND_STOP) { if ((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY) || /* Is the bus busy? */ \ ((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || \ (DeviceDataPrv->OutLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } else { if (((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || /* Is the bus busy? */ \ (DeviceDataPrv->OutLenM != 0x00U)) { return ERR_BUSY; /* If yes then error */ } } /* {MQXLite RTOS Adapter} Critical section begin (RTOS function call is defined by MQXLite RTOS Adapter property) */ _int_disable(); DeviceDataPrv->SerFlag |= MASTER_IN_PROGRES; /* Set flag "busy" */ DeviceDataPrv->OutPtrM = (uint8_t *)BufferPtr; /* Save pointer to data for transmitting */ DeviceDataPrv->OutLenM = Size; /* Set the counter of output bufer's content */ DeviceDataPrv->SendStop = SendStop; /* Set generating stop condition */ I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_TX_DIRECTION); /* Set TX mode */ if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */ I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* If yes then repeat start cycle generated */ } else { I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_MASTER_MODE); /* If no then start signal generated */ } I2C_PDD_WriteDataReg(I2C0_BASE_PTR, 0x00U); /* Send slave address */ /* {MQXLite RTOS Adapter} Critical section ends (RTOS function call is defined by MQXLite RTOS Adapter property) */ _int_enable(); return ERR_OK; /* OK */ }
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? } }