/** ******************************************************************************* * * @file sensors.c * @authors fatih.bellachia@lapp.in2p3.fr * @date 30/09/2015 * @version v0r1 * @brief Example of sensors registration in Resource Broker. * ******************************************************************************* */ /* Includes ------------------------------------------------------------------*/ #include #include #include #include #include #include #include "cfg_data.h" /** @addtogroup Component * @{ */ /** @defgroup Sensors * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint32_t auiChannel2Bus[CHANNEL_NUMBER] = { IPMB_0, -1, I2C_MGT, I2C_SENSOR, I2C_USR, I2C_IPM_IO, -1, IPMB_L, -1, -1, -1, -1, -1, -1, -1, -1 }; /* Private function prototypes -----------------------------------------------*/ extern int ad7414Config(uint32_t i2cChannel, u8 address); extern int ad7414Read(uint32_t i2cChannel, u8 address, u8 dataChannel); extern int ltc4151Config(uint32_t i2cChannel, u8 address); extern int ltc4151Read(uint32_t i2cChannel, u8 address, u8 dataChannel); extern int iq65033Config(uint32_t i2cChannel, u8 address); extern int iq65033Read(uint32_t i2cChannel, u8 address, u8 dataChannel); /* Public variables ----------------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ static bool InitSensorAD7414(struct RBResource *pstResource) /*----------------------------------------------------------------------------*/ { // Sanity check if (pstResource == NULL) return false; // The four high-order bits of I2C address for the AD7414/AD7415 are set to 1001 if ((pstResource->ucAddress & 0x48) != 0x48) return false; uint32_t uiI2Cx = auiChannel2Bus[pstResource->ucChannelId]; if (uiI2Cx == 0xFFFFFFFF) return false; return (ad7414Config(uiI2Cx, pstResource->ucAddress) != -1); } /*----------------------------------------------------------------------------*/ static bool ReadSensorAD7414(struct RBResource *pstResource, uint8_t *pucData) /*----------------------------------------------------------------------------*/ { // Sanity check if ((pstResource == NULL) || (pucData == NULL)) return false; // The four high-order bits of I2C address for the AD7414/AD7415 are set to 1001 if ((pstResource->ucAddress & 0x48) != 0x48) return false; int iRtn; uint32_t uiI2Cx = auiChannel2Bus[pstResource->ucChannelId]; if (uiI2Cx == 0xFFFFFFFF) return false; if ((iRtn = ad7414Read(uiI2Cx, pstResource->ucAddress, 0)) == -1) return false; // ADC 10-bit result -> ADC 8-bit *pucData = (iRtn & 0x000003FF) >> 2; return true; } /*----------------------------------------------------------------------------*/ static int WriteSensorAD7414(struct RBResource *pstResource, void *pvBuffer, uint32_t uiLength) /*----------------------------------------------------------------------------*/ { // Sanity check if ((pstResource == NULL) || (pvBuffer == NULL)) return -1; // The four high-order bits of I2C address for the AD7414/AD7415 are set to 1001 if ((pstResource->ucAddress & 0x48) != 0x48) return -1; // Nothing to do... return uiLength; } /*----------------------------------------------------------------------------*/ static bool InitSensorLTC4151(struct RBResource *pstResource) /*----------------------------------------------------------------------------*/ { // Sanity check if (pstResource == NULL) return false; // The three high-order bits of I2C address for the LTC4151 are set to 110 // Address range checking if ((pstResource->ucAddress < 0x68) || (pstResource->ucAddress > 0x6F)) return false; uint32_t uiI2Cx = auiChannel2Bus[pstResource->ucChannelId]; if (uiI2Cx == 0xFFFFFFFF) return false; return (ltc4151Config(uiI2Cx, pstResource->ucAddress) != -1); } /*----------------------------------------------------------------------------*/ static bool ReadSensorLTC4151(struct RBResource *pstResource, uint8_t *pucData) /*----------------------------------------------------------------------------*/ { // Sanity check if ((pstResource == NULL) || (pucData == NULL)) return false; // The three high-order bits of I2C address for the LTC4151 are set to 110 // Address range checking if ((pstResource->ucAddress < 0x68) || (pstResource->ucAddress > 0x6F)) return false; int iRtn; uint32_t uiI2Cx = auiChannel2Bus[pstResource->ucChannelId]; uint8_t ucIdx = pstResource->ucIdentifier - SDR_NUM_LTC4151_12V_CURRENT; if (uiI2Cx == 0xFFFFFFFF) return false; if ((iRtn = ltc4151Read(uiI2Cx, pstResource->ucAddress, ucIdx)) == -1) return false; // ADC 12-bit result -> ADC 8-bit *pucData = (iRtn & 0x00000FFF) >> 4; return true; } /*----------------------------------------------------------------------------*/ static int WriteSensorLTC4151(struct RBResource *pstResource, void *pvBuffer, uint32_t uiLength) /*----------------------------------------------------------------------------*/ { // Sanity check if ((pstResource == NULL) || (pvBuffer == NULL)) return -1; // The three high-order bits of I2C address for the LTC4151 are set to 110 // Address range checking if ((pstResource->ucAddress < 0x68) || (pstResource->ucAddress > 0x6F)) return -1; // Nothing to do... return uiLength; } /*----------------------------------------------------------------------------*/ static bool InitSensorIQ65033(struct RBResource *pstResource) /*----------------------------------------------------------------------------*/ { // Sanity check if (pstResource == NULL) return false; // The four high-order bits of I2C address for the IQ65033 are set to 0101 if ((pstResource->ucAddress & 0x28) != 0x28) return false; uint32_t uiI2Cx = auiChannel2Bus[pstResource->ucChannelId]; if (uiI2Cx == 0xFFFFFFFF) return false; return (iq65033Config(uiI2Cx, pstResource->ucAddress) != -1); } /*----------------------------------------------------------------------------*/ static bool ReadSensorIQ65033(struct RBResource *pstResource, uint8_t *pucData) /*----------------------------------------------------------------------------*/ { // Sanity check if ((pstResource == NULL) || (pucData == NULL)) return false; // The four high-order bits of I2C address for the IQ65033 are set to 0101 if ((pstResource->ucAddress & 0x28) != 0x28) return false; int iRtn; uint32_t uiI2Cx = auiChannel2Bus[pstResource->ucChannelId]; uint8_t ucIdx = pstResource->ucIdentifier - SDR_NUM_IQ65033_STATUS; if (uiI2Cx == 0xFFFFFFFF) return false; if ((iRtn = iq65033Read(uiI2Cx, pstResource->ucAddress, ucIdx)) == -1) return false; *pucData = (uint8_t)iRtn; return true; } /*----------------------------------------------------------------------------*/ static int WriteSensorIQ65033(struct RBResource *pstResource, void *pvBuffer, uint32_t uiLength) /*----------------------------------------------------------------------------*/ { // Sanity check if ((pstResource == NULL) || (pvBuffer == NULL)) return -1; // The four high-order bits of I2C address for the IQ65033 are set to 0101 if ((pstResource->ucAddress & 0x28) != 0x28) return -1; // Nothing to do... return uiLength; } /* Public functions ----------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ bool SensorsInit() /*----------------------------------------------------------------------------*/ { RBResource_t stResource; // AD7414 Temperature stResource.ucChannelId = CHANNEL_I2C_SENSOR; stResource.ucAddress = SDR_AD7414_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_AD7414_TEMP; stResource.fnInit = InitSensorAD7414; stResource.fnRead = ReadSensorAD7414; stResource.fnWrite = WriteSensorAD7414; if (ResourceBrokerAddResource("AD7414 Temp", &stResource) == false) return false; // LTC4151 12V Current stResource.ucChannelId = CHANNEL_I2C_SENSOR; stResource.ucAddress = SDR_LTC4151_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_LTC4151_12V_CURRENT; stResource.fnInit = InitSensorLTC4151; stResource.fnRead = ReadSensorLTC4151; stResource.fnWrite = WriteSensorLTC4151; if (ResourceBrokerAddResource("LTC4151 12V Current", &stResource) == false) return false; // LTC4151 12V Voltage stResource.ucChannelId = CHANNEL_I2C_SENSOR; stResource.ucAddress = SDR_LTC4151_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_LTC4151_12V_VOLTAGE; stResource.fnInit = InitSensorLTC4151; stResource.fnRead = ReadSensorLTC4151; stResource.fnWrite = WriteSensorLTC4151; if (ResourceBrokerAddResource("LTC4151 12V Voltage", &stResource) == false) return false; // IQ65033 HU_CAP Voltage stResource.ucChannelId = CHANNEL_I2C_MGT; stResource.ucAddress = SDR_IQ65033_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_IQ65033_48V_CURRENT; stResource.fnInit = InitSensorIQ65033; stResource.fnRead = ReadSensorIQ65033; stResource.fnWrite = WriteSensorIQ65033; if (ResourceBrokerAddResource("IQ65033 HU_CAP Voltage", &stResource) == false) return false; // IQ65033 -48V Current stResource.ucChannelId = CHANNEL_I2C_MGT; stResource.ucAddress = SDR_IQ65033_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_IQ65033_48V_CURRENT; stResource.fnInit = InitSensorIQ65033; stResource.fnRead = ReadSensorIQ65033; stResource.fnWrite = WriteSensorIQ65033; if (ResourceBrokerAddResource("IQ65033 -48V Current", &stResource) == false) return false; // IQ65033 -48V_A Voltage stResource.ucChannelId = CHANNEL_I2C_MGT; stResource.ucAddress = SDR_IQ65033_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_IQ65033_48V_A; stResource.fnInit = InitSensorIQ65033; stResource.fnRead = ReadSensorIQ65033; stResource.fnWrite = WriteSensorIQ65033; if (ResourceBrokerAddResource("IQ65033 -48V_A Voltage", &stResource) == false) return false; // IQ65033 -48V_B Voltage stResource.ucChannelId = CHANNEL_I2C_MGT; stResource.ucAddress = SDR_IQ65033_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_IQ65033_48V_B; stResource.fnInit = InitSensorIQ65033; stResource.fnRead = ReadSensorIQ65033; stResource.fnWrite = WriteSensorIQ65033; if (ResourceBrokerAddResource("IQ65033 -48V_B Voltage", &stResource) == false) return false; // IQ65033 Temperature stResource.ucChannelId = CHANNEL_I2C_MGT; stResource.ucAddress = SDR_IQ65033_I2C_ADDR; stResource.ucIdentifier = SDR_NUM_IQ65033_TEMP; stResource.fnInit = InitSensorIQ65033; stResource.fnRead = ReadSensorIQ65033; stResource.fnWrite = WriteSensorIQ65033; if (ResourceBrokerAddResource("IQ65033 Temp", &stResource) == false) return false; ResourceBrokerDumpResources("DEBUG> [SensorsInit]"); return true; } /*----------------------------------------------------------------------------*/ bool SensorsClean() /*----------------------------------------------------------------------------*/ { bool bRtn = true; bRtn &= ResourceBrokerRemoveResourceByName("AD7414 Temp"); bRtn &= ResourceBrokerRemoveResourceByName("LTC4151 12V Current"); bRtn &= ResourceBrokerRemoveResourceByName("LTC4151 12V Voltage"); bRtn &= ResourceBrokerRemoveResourceByName("IQ65033 HU_CAP Voltage"); bRtn &= ResourceBrokerRemoveResourceByName("IQ65033 -48V Current"); bRtn &= ResourceBrokerRemoveResourceByName("IQ65033 -48V_A Voltage"); bRtn &= ResourceBrokerRemoveResourceByName("IQ65033 -48V_B Voltage"); bRtn &= ResourceBrokerRemoveResourceByName("IQ65033 Temp"); return bRtn; } MODULE_BEGIN_DECL(sensors, 1) MODULE_NAME("Board sensors") MODULE_AUTHOR("fatih.bellachia@lapp.in2p3.fr") MODULE_INIT(SensorsInit) MODULE_CLEANUP(SensorsClean) MODULE_PROCESS(NULL, NULL) MODULE_END_DECL /** * @} */ /** * @} */