/* ** =================================================================== ** Method : DrvI2c0_CheckBus (component I2C_LDD) ** ** Description : ** This method returns the status of the bus. If the START ** condition has been detected, the method returns LDD_I2C_BUSY. ** If the STOP condition has been detected, the method returns ** LDD_I2C_IDLE. ** Parameters : ** NAME - DESCRIPTION ** * DeviceDataPtr - Device data structure ** pointer returned by <Init> method. ** * BusStatePtr - Pointer to a variable, ** where value of status is stored. ** Returns : ** --- - Error code, possible codes: ** ERR_OK - OK ** ERR_DISABLED - Device is disabled ** ERR_SPEED - This device does not work in ** the active clock configuration ** =================================================================== */ LDD_TError DrvI2c0_CheckBus(LDD_TDeviceData *DeviceDataPtr, LDD_I2C_TBusState *BusStatePtr) { DrvI2c0_TDeviceData *DeviceDataPrv = (DrvI2c0_TDeviceData *)DeviceDataPtr; (void)DeviceDataPrv; /* Suppress unused variable warning if needed */ *BusStatePtr = (LDD_I2C_TBusState)((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY)?LDD_I2C_BUSY:LDD_I2C_IDLE); /* Return value of Busy bit in status register */ return ERR_OK; }
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 */ }
/* ===================================================================*/ 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 */ }