Middleware to implement the HS300X sensor interface. This module implements the HS300X Middleware Interface.
Overview
Features
The HS300X sensor interface implementation has the following key features:
- Starting a measurement at any time
- Getting ADC data from the sensor
- Calculating humidity and temperature value from getting ADC data
- Changing the sensor resolution
- Getting the sensor ID
Configuration
Build Time Configurations for rm_hs300x
The following build time configurations are defined in fsp_cfg/rm_hs300x_cfg.h:
Configuration | Options | Default | Description |
Parameter Checking |
-
Default (BSP)
-
Enabled
-
Disabled
| Default (BSP) | If selected code for parameter checking is included in the build. |
Data type |
-
Both humidity and temperature
-
Humidity only
| Both humidity and temperature | Select Getting humidity only and both humidity and temperature. |
Programming Mode |
| OFF | If selected the programming mode can be entered. |
Configurations for Sensor > HS300X Temperature/Humidity Sensor (rm_hs300x)
This module can be added to the Stacks tab via New Stack > Sensor > HS300X Temperature/Humidity Sensor (rm_hs300x).
Configuration | Options | Default | Description |
Name | Name must be a valid C symbol | g_hs300x_sensor0 | Module name. |
Callback | Name must be a valid C symbol | hs300x_callback | A user callback function can be provided. |
Pin Configuration
This module use SDA and SCL pins of I2C Master and SCI I2C.
Usage Notes
HS300x datasheet is here.
If ADC data is valid and calculating humidity and temperature is finished, it is needed to start a measurement again. If ADC data is invalid, it is needed to read ADC data from HS300x again.
If changing the sensor resolution and getting the sensor ID, RM_HS300X_ProgrammingModeEnter function must be called within 10ms after applying power to the sensor. Entering the programming mode takes 120us. Thresore, after calling RM_HS300X_ProgrammingModeEnter function, please wait 120us. After calling RM_HS300X_ResolutionChange function, 14ms must be waited because failure to comply with these times may result in data corruption and introduce errors in sensor measurements.
If an RTOS is used, blocking and bus lock is available.
- If blocking of an I2C bus is required, it is necessary to create a semaphore for blocking.
- If bus lock is required, it is necessary to create a mutex for bus lock. Bus lock is only available when a semaphore for blocking is used.
Bus Initialization
The HS300X interface expects a bus instance to be opened before opening any HS300X device. The interface will handle switching between devices on the bus but will not open or close the bus instance. The user should open the bus with the appropriate I2C Master Interface open call.
Examples
Basic Example
This is a basic example of minimal use of HS300X sensor implementation in an application.
void rm_hs300x_basic_example (void)
{
uint8_t calculated_flag = 0;
rm_comms_i2c_bus_extended_cfg_t * p_extend =
(rm_comms_i2c_bus_extended_cfg_t *) g_hs300x_cfg.p_instance->p_cfg->p_extend;
p_driver_instance->
p_api->
open(p_driver_instance->p_ctrl, p_driver_instance->p_cfg);
#if BSP_CFG_RTOS
if (NULL != p_extend->p_blocking_semaphore)
{
#if BSP_CFG_RTOS == 1 // AzureOS
tx_semaphore_create(p_extend->p_blocking_semaphore->p_semaphore_handle,
p_extend->p_blocking_semaphore->p_semaphore_name,
(ULONG) 0);
#elif BSP_CFG_RTOS == 2 // FreeRTOS
*(p_extend->p_blocking_semaphore->p_semaphore_handle) =
xSemaphoreCreateCountingStatic((UBaseType_t) 1,
(UBaseType_t) 0,
p_extend->p_blocking_semaphore->p_semaphore_memory);
#endif
}
if (NULL != p_extend->p_bus_recursive_mutex)
{
#if BSP_CFG_RTOS == 1 // AzureOS
tx_mutex_create(p_extend->p_bus_recursive_mutex->p_mutex_handle,
p_extend->p_bus_recursive_mutex->p_mutex_name,
TX_INHERIT);
#elif BSP_CFG_RTOS == 2 // FreeRTOS
*(p_extend->p_bus_recursive_mutex->p_mutex_handle) =
xSemaphoreCreateRecursiveMutexStatic(p_extend->p_bus_recursive_mutex->p_mutex_memory);
#endif
}
#endif
handle_error(err);
#if RM_HS300X_CFG_PROGRAMMING_MODE
uint32_t sensor_id;
g_flag = 0;
while (0 == g_flag)
{
}
g_flag = 0;
while (0 == g_flag)
{
}
g_flag = 0;
while (0 == g_flag)
{
}
g_flag = 0;
while (0 == g_flag)
{
}
#endif
while (true)
{
g_flag = 0;
while (0 == g_flag)
{
}
do
{
g_flag = 0;
while (0 == g_flag)
{
}
if (FSP_SUCCESS == err)
{
calculated_flag = 1;
}
{
calculated_flag = 0;
}
else
{
handle_error(err);
}
} while (0 == calculated_flag);
}
}
◆ rm_hs300x_programmnig_mode_params_t
struct rm_hs300x_programmnig_mode_params_t |
HS300X programming mode process block
Data Fields |
volatile bool |
enter |
Enter flag. |
volatile bool |
blocking |
Blocking flag. |
volatile bool |
communication_finished |
Communication flag for blocking. |
volatile rm_hs300x_event_t |
event |
Callback event. |
◆ rm_hs300x_instance_ctrl_t
struct rm_hs300x_instance_ctrl_t |
◆ RM_HS300X_Open()
Opens and configures the HS300X Middle module. Implements rm_hs300x_api_t::open.
Example:
- Return values
-
FSP_SUCCESS | HS300X successfully configured. |
FSP_ERR_ASSERTION | Null pointer, or one or more configuration options is invalid. |
FSP_ERR_ALREADY_OPEN | Module is already open. This module can only be opened once. |
◆ RM_HS300X_Close()
Disables specified HS300X control block. Implements rm_hs300x_api_t::close.
- Return values
-
FSP_SUCCESS | Successfully closed. |
FSP_ERR_ASSERTION | Null pointer passed as a parameter. |
FSP_ERR_NOT_OPEN | Module is not open. |
◆ RM_HS300X_MeasurementStart()
This function should be called when start a measurement and when measurement data is stale data. Sends the slave address to the hs300x and start a measurement. Implements rm_hs300x_api_t::measurementStart.
- Return values
-
FSP_SUCCESS | Successfully started. |
FSP_ERR_ASSERTION | Null pointer passed as a parameter. |
FSP_ERR_NOT_OPEN | Module is not open. |
◆ RM_HS300X_Read()
Reads ADC data from HS300X. Implements rm_hs300x_api_t::read.
- Return values
-
FSP_SUCCESS | Successfully data decoded. |
FSP_ERR_ASSERTION | Null pointer, or one or more configuration options is invalid. |
FSP_ERR_NOT_OPEN | Module is not open. |
◆ RM_HS300X_DataCalculate()
Calculates humidity [RH] and temperature [Celsius] from ADC data. Implements rm_hs300x_api_t::dataCalculate.
- Return values
-
FSP_SUCCESS | Successfully data decoded. |
FSP_ERR_ASSERTION | Null pointer, or one or more configuration options is invalid. |
FSP_ERR_NOT_OPEN | Module is not open. |
FSP_ERR_SENSOR_INVALID_DATA | Data is invalid. |
◆ RM_HS300X_ProgrammingModeEnter()
This function must be called within 10ms after applying power to the sensor. Sends the commands to enter the programming mode. After calling this function, please wait 120us. Implements rm_hs300x_api_t::programmingModeEnter.
- Return values
-
FSP_SUCCESS | Successfully started. |
FSP_ERR_ASSERTION | Null pointer passed as a parameter. |
FSP_ERR_NOT_OPEN | Module is not open. |
FSP_ERR_UNSUPPORTED | Programming mode is not supported. |
◆ RM_HS300X_ResolutionChange()
This function must be called after calling the RM_HS300X_ProgrammingModeEnter function. Changes the sensor resolution. This function blocks for 120 us software delay plus 9 bytes on the I2C bus. After calling this function, 14ms must be waited. Failure to comply with these times may result in data corruption and introduce errors in sensor measurements. Implements rm_hs300x_api_t::resolutionChange.
- Return values
-
FSP_SUCCESS | Successfully started. |
FSP_ERR_ASSERTION | Null pointer passed as a parameter. |
FSP_ERR_NOT_OPEN | Module is not open. |
FSP_ERR_INVALID_MODE | Module is not entering the programming mode. |
FSP_ERR_ABORTED | Communication is aborted. |
FSP_ERR_TIMEOUT | Communication is timeout. |
FSP_ERR_UNSUPPORTED | Programming mode is not supported. |
◆ RM_HS300X_SensorIdGet()
This function must be called after calling the RM_HS300X_ProgrammingModeEnter function. Gets the sensor ID. This function blocks for 240 us software delay plus 12 bytes on the I2C bus. Implements rm_hs300x_api_t::sensorIdGet.
- Return values
-
FSP_SUCCESS | Successfully started. |
FSP_ERR_ASSERTION | Null pointer passed as a parameter. |
FSP_ERR_NOT_OPEN | Module is not open. |
FSP_ERR_INVALID_MODE | Module is not entering the programming mode. |
FSP_ERR_ABORTED | Communication is aborted. |
FSP_ERR_TIMEOUT | Communication is timeout. |
FSP_ERR_UNSUPPORTED | Programming mode is not supported. |
◆ RM_HS300X_ProgrammingModeExit()
This function must be called after calling the RM_HS300X_ProgrammingModeEnter function. This function must be called to return to normal sensor operation and perform measurements. Sends the commands to exit the programming mode. Implements rm_hs300x_api_t::programmingModeExit.
- Return values
-
FSP_SUCCESS | Successfully started. |
FSP_ERR_ASSERTION | Null pointer passed as a parameter. |
FSP_ERR_NOT_OPEN | Module is not open. |
FSP_ERR_INVALID_MODE | Module is not entering the programming mode. |
FSP_ERR_UNSUPPORTED | Programming mode is not supported. |