SENSOR Devices
Last updated
Last updated
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University
Sensor is an important part of the Internet of Things. "Sensor" is to the Internet of Things as "eyes are to humans". Without eyes, humans cannot see the world, and the same is true for the Internet of Things.
With the development of the Internet of Things, a large number of sensors have been developed for developers to choose from, such as accelerometers, magnetometers, gyroscopes, barometers/pressure, and humidometers. These sensors are produced by major semiconductor manufacturers in the world. Although they increase the market's selectivity, they also increase the difficulty of application development. Because different sensor manufacturers and different sensors need to be equipped with their own unique drivers to operate, when developing applications, they need to adapt to different sensors, which naturally increases the difficulty of development. In order to reduce the difficulty of application development and increase the reusability of sensor drivers, we designed sensor devices.
The function of the sensor device is to provide a unified operation interface for the upper layer and improve the reusability of the upper layer code.
Interface: standard device interface (open/close/read/control)
Working mode: Support polling, interrupt and FIFO modes
Power mode: supports four modes: power off, normal, low power consumption, and high power consumption
Click the sensor list to view currently supported sensors.
The application accesses the sensor device through the I/O device management interface provided by RT-Thread. The relevant interface is as follows:
The application obtains the device handle according to the sensor device name, and then can operate the sensor device. The device search function is as follows:
The usage examples are as follows:
Through the device handle, the application can open and close the device. When opening the device, it will detect whether the device has been initialized. If it has not been initialized, the initialization interface will be called by default to initialize the device. Open the device through the following function:
The oflags parameter supports the following parameters:
There are three modes for receiving and sending sensor data: interrupt mode, polling mode, and FIFO mode. When using, only one of these three modes can be selected . If the sensor's opening parameter oflags does not specify the interrupt mode or FIFO mode, the polling mode is used by default.
FIFO (First Input First Output) means first in, first out. FIFO transmission mode requires sensor hardware support. Data is stored in the hardware FIFO. Multiple data can be read at one time, which saves CPU resources for other operations. It is very useful in low power mode.
If the sensor is to use FIFO receive mode, oflags takes the value RT_DEVICE_FLAG_FIFO_RX.
An example of using a sensor device in polling mode is as follows:
Through the command control word, the application can configure the sensor device through the following functions:
The cmd currently supports the following command control characters:
The units used when setting the sensor's measurement range are the units provided when the device was registered.
Set the output rate to 100HZ and call the following interface.
The data receiving indication can be set through the following function. When the sensor receives data, it notifies the upper application thread that data has arrived:
The callback function of this function is provided by the caller. If the sensor is turned on in interrupt receiving mode, when the sensor receives data and generates an interrupt, the callback function will be called, and the data size of the buffer at this time will be placed in the size parameter, and the sensor device handle will be placed in the dev parameter for the caller to obtain.
In general, the receiving callback function can send a semaphore or event to notify the sensor data processing thread that data has arrived. The usage example is as follows:
The following function can be called to read the data received by the sensor:
The following is an example of using the sensor in interrupt reception mode and in conjunction with the reception callback function:
The following is an example of using the sensor in FIFO receiving mode and in conjunction with the receiving callback function:
When the application completes the sensor operation, it can close the sensor device through the following function:
Closing the device interface and opening the device interface must be used in pairs. Each time you open the device, you must close it once. Only in this way can the device be completely closed. Otherwise, the device will still be in an open state.
The specific usage of the sensor device can refer to the following sample code. The main steps of the sample code are as follows:
First look up the sensor settings to get the device handle.
Turn on the sensor in polling mode.
Read the data 5 times continuously and print it out.
Turn off the sensor.
This sample code is not limited to a specific BSP. It can be run by entering a different dev_name according to the sensor device registered in the BSP.
function
describe
rt_device_find()
Find the device and get the device handle according to the sensor device name
rt_device_open()
Open the sensor device
rt_device_read()
Reading Data
rt_device_control()
Controlling sensor devices
rt_device_set_rx_indicate()
Set the receiving callback function
rt_device_close()
Turn off sensor devices
parameter
describe
name
Sensor device name
return
——
Device handle
If the corresponding device is found, the corresponding device handle will be returned.
RT_NULL
No corresponding device object was found
parameter
describe
dev
Device handle
oflags
Device mode flags
return
——
RT_EOK
Device opened successfully
- RT_EBUSY
If the device registration parameter includes the RT_DEVICE_FLAG_STANDALONE parameter, the device will not be allowed to be opened repeatedly.
-RT_EINVAL
Unsupported open parameters
Other error codes
Device open failed
parameter
describe
dev
Device handle
cmd
Command control word, see below for details
arg
Control parameters, detailed description see below
return
——
RT_EOK
Function execution successful
-RT_ENOSYS
Execution failed, dev is empty
Other error codes
Execution failed
parameter
describe
dev
Device handle
rx_ind
Callback function pointer
dev
Device handle (callback function parameter)
size
Buffer data size (callback function parameter)
return
——
RT_EOK
Set up for success
parameter
describe
dev
Device handle
pos
Read data offset. This parameter is not used by the sensor.
buffer
Buffer pointer. The read data will be saved in the buffer.
size
The number of data read
return
——
>0
Returns the number of data read
0
You need to read the errno of the current thread to determine the error status
parameter
describe
dev
Device handle
return
——
RT_EOK
Shut down the device successfully
-RT_ERROR
The device has been completely shut down. You cannot shut down the device again.
Other error codes
Failed to shut down the device