/** * @brief Set TSENSOR Initialization. * @param DeviceAddr : Device ID address. * @param pInitStruct: pointer to a TSENSOR_InitTypeDef structure * that contains the configuration setting for the TSENSOR. * @retval TSENSOR status */ TSENSOR_StatusTypeDef TSENSOR_Init(uint16_t DeviceAddr, TSENSOR_InitTypeDef *pInitStruct) { uint8_t buffertx[2] = {0,0}; /*##-1- Initialise the SMBUS peripheral ####################################*/ SmbusHandle.Instance = TSENSOR_SMBUS; SmbusHandle.Init.Timing = TSENSOR_TIMING; SmbusHandle.Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLED; SmbusHandle.Init.OwnAddress1 = 0x00; SmbusHandle.Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT; SmbusHandle.Init.DualAddressMode = SMBUS_DUALADDRESS_DISABLED; SmbusHandle.Init.OwnAddress2 = 0x00; SmbusHandle.Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLED; SmbusHandle.Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLED; SmbusHandle.Init.PacketErrorCheckMode = SMBUS_PEC_DISABLED; SmbusHandle.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_HOST; SmbusHandle.Init.SMBusTimeout = 0; if(HAL_SMBUS_Init(&SmbusHandle) != HAL_OK) { return TSENSOR_ERROR; } /*##-2- Verify that Temperature Sensor is ready ############################*/ if (TSENSOR_IsReady(DeviceAddr, 20) != TSENSOR_OK ) { return TSENSOR_ERROR; } /*##-3- Set the Configuration Register #####################################*/ buffertx[0] = TSENSOR_CONFIGURATION; buffertx[1] = (uint8_t)(pInitStruct->AlertMode | pInitStruct->ConversionMode | pInitStruct->ConversionResolution); if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr, &buffertx[0], sizeof(buffertx), SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-4- Set the Conversion Rate ############################################*/ buffertx[0] = TSENSOR_CONVERSION_RATE; buffertx[1] = (uint8_t)(pInitStruct->ConversionRate); if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr ,&buffertx[0], sizeof(buffertx), SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-5- Set the HIGH Limit Temperature #####################################*/ buffertx[0] = TSENSOR_TEMP_HIGH_LIMIT_HIGH_BYTE; buffertx[1] = (uint8_t)(pInitStruct->TemperatureLimitHigh); if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr, &buffertx[0], sizeof(buffertx), SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-6- Set the low Limit Temperature ######################################*/ buffertx[0] = TSENSOR_TEMP_LOW_LIMIT_HIGH_BYTE; buffertx[1] = (uint8_t)(pInitStruct->TemperatureLimitLow); if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr, &buffertx[0], sizeof(buffertx), SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-7- Manage Alert Mode setting ##########################################*/ /* Clear an old alert if limit status is set */ /* To clear an alert, need to perform an Alert Response command */ if(((TSENSOR_ReadStatus(DeviceAddr) & TSENSOR_TEMP_EXCEED_HIGH_LIMIT) == TSENSOR_TEMP_EXCEED_HIGH_LIMIT) || ((TSENSOR_ReadStatus(DeviceAddr) & TSENSOR_TEMP_IS_OR_BELOW_LOW_LIMIT) == TSENSOR_TEMP_IS_OR_BELOW_LOW_LIMIT)) { TSENSOR_AlerteResponseAddressRead(); } /* Check Alert Mode requested */ if(pInitStruct->AlertMode == TSENSOR_ALERT_ENABLE) { /* Enable SMBUS Alert */ HAL_SMBUS_EnableAlert_IT(&SmbusHandle); } /* Return status OK*/ return TSENSOR_OK; }
/** * @brief Set TSENSOR Initialization. * @param DeviceAddr : Device ID address. * @param pInitStruct: pointer to a TSENSOR_InitTypeDef structure * that contains the configuration setting for the TSENSOR. * @retval TSENSOR status */ TSENSOR_StatusTypeDef TSENSOR_Init(uint16_t DeviceAddr, TSENSOR_InitTypeDef *pInitStruct) { uint8_t buffertx[3] = {0,0,0}; /*##-1- Initialise the SMBUS peripheral ####################################*/ SmbusHandle.Instance = TSENSOR_SMBUS; SmbusHandle.Init.Timing = TSENSOR_TIMING; SmbusHandle.Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLE; SmbusHandle.Init.OwnAddress1 = 0x00; SmbusHandle.Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT; SmbusHandle.Init.DualAddressMode = SMBUS_DUALADDRESS_DISABLE; SmbusHandle.Init.OwnAddress2 = 0x00; SmbusHandle.Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLE; SmbusHandle.Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLE; SmbusHandle.Init.PacketErrorCheckMode = SMBUS_PEC_DISABLE; SmbusHandle.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_HOST; SmbusHandle.Init.SMBusTimeout = 0; if(HAL_SMBUS_Init(&SmbusHandle) != HAL_OK) { return TSENSOR_ERROR; } /*##-2- Verify that Temperature Sensor is ready ############################*/ if (TSENSOR_IsReady(DeviceAddr, 20) != TSENSOR_OK ) { return TSENSOR_ERROR; } /*##-3- Set the Configuration Register #####################################*/ buffertx[0] = TSENSOR_REG_CONF; buffertx[1] = (uint8_t)(pInitStruct->AlertMode | pInitStruct->ConversionMode); if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr, &buffertx[0], /*sizeof(buffertx)*/2, SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-4- Set the HIGH Limit Temperature #####################################*/ buffertx[0] = TSENSOR_REG_TOS; buffertx[1] = (uint8_t)(pInitStruct->TemperatureLimitHigh); buffertx[2] = 0; if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr, &buffertx[0], /*sizeof(buffertx)*/3, SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-5- Set the low Limit Temperature ######################################*/ buffertx[0] = TSENSOR_REG_THYS; buffertx[1] = (uint8_t)(pInitStruct->TemperatureLimitLow); buffertx[2] = 0; if (HAL_SMBUS_Master_Transmit_IT(&SmbusHandle, DeviceAddr, &buffertx[0], /*sizeof(buffertx)*/3, SMBUS_FIRST_AND_LAST_FRAME_NO_PEC) != HAL_OK) { return TSENSOR_ERROR; } while(HAL_SMBUS_GetState(&SmbusHandle) != HAL_SMBUS_STATE_READY); /*##-6- Check Alert Mode requested #########################################*/ if(pInitStruct->AlertMode == TSENSOR_INTERRUPT_MODE) { /* Enable SMBUS Alert */ HAL_SMBUS_EnableAlert_IT(&SmbusHandle); } /* Return status OK*/ return TSENSOR_OK; }