Abstract: This article brings you the application of LiteOS ultrasonic module porting based on Sensorhub.

1, the Sensor Hub

LiteOS Sensor Framework, namely Sensor Hub, is a Sensor management framework based on Huawei LiteOS Iot operating system.

With the development of the Internet of Things, the terminals of the Internet of Things are becoming more and more intelligent. For example, more and more sensors will be configured on terminals such as personal wearables, smart homes and home medical services to obtain more sensing data, making the terminals more intelligent and making development and maintenance more complex and difficult. The LiteOS sensor framework manages different types of sensors such as Accelerometer, Gyroscope, Barometer, and Humidometer on the Internet of Things terminal devices. Shielding its hardware details makes it “hardware” independent, which is very convenient for the development, maintenance and function expansion of Internet of Things devices.

LiteOS Sensor framework includes Sensor Manager, BSP Manager, and Converged Algorithms.

  • Sensor Manager: provides unified Sensor interaction management, such as Sensor configuration, sampling, reporting, and management.
  • BSP Manager: a unified driver interface, responsible for Sensor driver management, power management and interactive Sensor management, such as Sensor opening, closing, reading and writing, data update, etc.
  • Converged Algorithms: Fusion algorithms library (algorithm) based on the specific business model, according to the specific business model, on the side of the MCU to fusion algorithms, such as environment monitoring, step algorithm, from the traditional, simple acquisition algorithm to upgrade to the intelligent algorithm, direct call, promote business of sensor data, reduce the data acquisition time delay.

2. SR04 ultrasonic module

Hc-sr04 ultrasonic ranging module can provide 2cm-400cm non-contact distance sensing function, ranging accuracy up to 3mm; The module includes ultrasonic transmitter, receiver and control circuit. Basic working principle :(1) using IO port TRIG trigger ranging, to at least 10us high level signal; (2) The module automatically sends 8 40khz square waves to automatically detect whether any signal is returned; (3) If there is a signal returned, ECHO output a high level through the IO port. The duration of high level is the time of ultrasonic wave from transmitting to returning. Test distance =(high level time sound velocity (340M/S))/2; 2. Physical drawing: as shown in the wiring diagram on the right, VCC provides 5V power supply, GND is ground wire, TRIG trigger control signal input, ECHO ECHO signal output and other four branches. 3. Electrical parameters: Electrical specifications hc-sr04 ultrasonic module operating voltage DC 5 V operating current 15 ma operating frequency 40Hz maximum range 4m nearest range 2cm measuring Angle 15 degrees input trigger signal TTL pulse output echo signal output TTL level signal, proportional to the range specifications dimensions 4520 x 15mm.

It is shown from the sequence diagram that you only need to provide a pulse trigger signal above 10uS, and the module will emit 8 cycle levels of 40kHz internally and detect the echo. Once the echo signal is detected, the echo signal is output. The pulse width of the echo signal is proportional to the measured distance. The distance can be calculated from the time interval between the transmitting signal and the received echo signal. Formula: uS/58= centimeters or uS/148= inches; Or: distance = high level time * sound speed (340M/S) /2; It is recommended that the measurement period be more than 60ms to prevent the influence of transmitting signal on reverberation signal.

3. Sensorhub hC-SR04 driver

A simple receiving procedure to read the sensor can be completed through the sequence diagram :(here, GPIOA1 and GPIOA4 are used as examples)

uint32_t hcsr04_read (void) { local_time=0; HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // pull the TRIG pin HIGH delay(2); // wait for 2 us HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // pull the TRIG pin HIGH delay(10); // wait for 10 us HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // pull the TRIG pin low // read the time for which the pin is high while (! (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4))); // wait for the ECHO pin to go high while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4)) // while the pin is high { local_time++;  // measure time for which the pin is high delay (1); } return local_time * .034/2; }Copy the code

4. Register the driver with SensorHub

Write iO first, initialize, open, close, and read data operations

STATIC INT32 SR04Init(SensorType *sensor) { (VOID)(sensor); GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin : PD11 */ GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : PD12 PD13 PD14 PD15 */ GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); return LOS_OK; } STATIC INT32 SR04ReadData(SensorType *sensor) { PRINTK("read datan"); INT32 *data = (INT32 *)sensor->sensorData; data[0] = hcsr04_read(); return LOS_OK; } STATIC INT32 SR04Open(SensorType *sensor, OpenParam *para) { UINT32 ret; (VOID)(para); SR04 *SR04 = (SR04 *)sensor->priv; if ((sensor->sensorStat == SENSOR_STAT_OPEN) && (sensor->interval == SR04Period)) { return LOS_OK; } if (SR04->gyroTimerId ! = INVALID_TIMER_ID) { ret = LOS_SwtmrDelete(SR04->gyroTimerId); SR04->gyroTimerId = INVALID_TIMER_ID; if (ret ! = LOS_OK) { PRINT_ERR("delete a timer failed! n"); return LOS_NOK; } } // creat a timer, first parameter is ticks. ret = LOS_SwtmrCreate(sensor->interval, LOS_SWTMR_MODE_PERIOD, (SWTMR_PROC_FUNC)GypoTimerFunc, &SR04->gyroTimerId, (UINT32)sensor); if (ret ! = LOS_OK) { PRINT_ERR("creat a timer failed! n"); return LOS_NOK; } ret = LOS_SwtmrStart(SR04->gyroTimerId); if (ret ! = LOS_OK) { PRINT_ERR("start timer errn"); } SR04Period = sensor->interval; PRINTK("SR04 on.n"); return LOS_OK; } STATIC INT32 SR04Close(SensorType *sensor) { UINT32 ret; if (sensor->sensorStat == SENSOR_STAT_CLOSE) { PRINT_DEBUG("sr04 has been closedn"); return LOS_OK; } __HAL_RCC_GPIOA_CLK_DISABLE(); PRINTK("SR04 off.n"); return LOS_OK; }Copy the code

The driver of the design is then registered with the framework

STATIC struct SensorOperation Sr04Ops = {
    .Init = SR04Init,
    .Open = SR04Open,
    .Close = SR04Close,
    .ReadData = SR04ReadData,
};

STATIC SensorType g_sensorSR04 = {
    .sensorOp = &Sr04Ops,
    .sensorData = &g_SR04Data,
    .sensorDataLen = sizeof(g_SR04Data),
    .priv = &g_SR04Priv,
    .tag = TAG_BEGIN,
    .cmd = CMD_CMN_INTERVAL_REQ,
    .interval = DEFAULT_INTERVAL,

};
VOID SR04Register(VOID)
{
    SensorRegister(&g_sensorSR04);
}
Copy the code

Register the driver with the Universal Sensor driver module. Today’s code migration is almost complete, followed by sensors and boards for verification

conclusion

This driver is a problem, that is, this is a blocking process when reading, after the interview, the need to design using timer and external interrupt to complete, you can change the blocking process to non-blocking, greatly improved efficiency, the subsequent explanation of how to use timer and external trigger interrupt to complete the driver design.

Click to follow, the first time to learn about Huawei cloud fresh technology ~