Sensor model updating method suitable for single-chip microcomputer
By binding the sensor model to a specific memory segment and mapping it to a fixed address region on the microcontroller, the problems of firmware redundancy and resource waste in sensor model upgrades are solved, and lightweight, highly reliable dynamic updates of sensor models are achieved.
Patent Information
- Application Number
- CN202511330614.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-17
- Publication Date
- 2026-01-30
AI Technical Summary
Upgrading existing sensor models is difficult to complete stably on microcontrollers, resulting in firmware redundancy, inconvenient upgrades, resource waste, and maintenance difficulties. In particular, it cannot run effectively on low-cost microcontroller platforms with limited resources.
The sensor model is bound to a specific memory segment of the microcontroller by a preset structure, and mapped to a fixed address area by a linker script. The main program directly accesses the sensor model interface, receives the upgrade file, erases it, and copies it to the specific memory segment to achieve dynamic updates of the sensor model.
It enables dynamic updates of sensor models, reduces Flash usage by more than 50%, reduces upgrade data volume by 80%, improves upgrade convenience, and is suitable for resource-constrained microcontroller platforms.
Smart Images

Figure CN121433698A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of wireless upgrade technology, and in particular to a sensor model update method suitable for microcontrollers. Background Technology
[0002] In traditional embedded sensor access systems, if multiple sensor types need to be supported, all sensor drivers, parsing, and adaptation code are often compiled into the firmware. This approach not only consumes a large amount of program storage space (Flash) but also makes firmware upgrades and maintenance complex, failing to meet the efficient configuration requirements of "one device for multiple types, replaceable as needed" in practice.
[0003] Another approach is based on an RTOS or file system, dynamically loading sensor driver libraries from external Flash or SD cards. However, this method relies on the operating system and file management mechanisms, consumes significant resources, and is unsuitable for resource-constrained, low-cost microcontroller platforms.
[0004] Most current sensor access systems use a static binding approach, requiring the system software to pre-integrate all potentially usable sensor drivers and determine which driver functions to call at runtime based on the sensor configuration. This approach presents the following problems: 1. Firmware redundancy: All driver modules need to be pre-compiled into the firmware, resulting in significant redundancy and wasting valuable program space; 2. Inconvenient upgrades: When the sensor model changes or business requirements are adjusted, the entire system firmware must be recompiled and flashed; 3. Wasteful resources: Even if only one type of sensor is used, space must be reserved for other unused modules; 4. Difficult maintenance: Each change of sensor model may involve modifications to the entire system, hindering rapid deployment.
[0005] To overcome the aforementioned problems, some high-end systems have introduced dynamic library loading mechanisms or operating system plugin frameworks. However, these mechanisms all rely on significant system resources and cannot run reliably on resource-constrained microcontrollers. Therefore, there is an urgent need for a lightweight, highly reliable sensor model management mechanism with dynamic upgrade capabilities, supporting microcontrollers to switch sensor models and expand functions during runtime based on external configurations or remote commands. Summary of the Invention
[0006] The purpose of this application is to provide a sensor model update method suitable for microcontrollers, thereby solving the technical problem that existing sensor model upgrades are difficult to stably complete on microcontrollers. The various technical effects of the preferred solutions among the many technical solutions provided in this application are detailed below.
[0007] To achieve the above objectives, this application provides the following technical solutions:
[0008] This application provides a sensor model update method applicable to a microcontroller, comprising: setting a sensor model according to a preset structure; binding each instance of the sensor model to a specific memory segment of the microcontroller through a compiler; the preset structure including a base function pointer and a substructure of the sensor model; the base function pointer including at least an initialization function pointer, a data reading function pointer, and a data parsing function pointer; the substructure including at least a monitoring part, a parameter configuration part, and an alarm threshold management part; mapping the sensor model code and data corresponding to the specific memory segment to a fixed address area of the microcontroller through a linker script; wherein the main program of the microcontroller directly accesses the sensor model interface through the fixed address area; receiving the upgrade file of the sensor model; erasing the specific memory segment; copying the data of the upgrade file to the specific memory segment; and calling the upgraded sensor model after restarting or reloading the microcontroller to complete the sensor model update.
[0009] In some embodiments, the method includes: compiling the source file corresponding to each sensor model to generate a target file; splitting the target file into several pages according to a preset number of bytes; adding preset content to each page; merging all pages into a .dat file according to the page number order to obtain the upgrade file, wherein the preset content includes the total number of pages, the current page number, the total file verification, the current page verification, the sensor model, the hardware version number, the software version number, and the file content.
[0010] In some embodiments, the method includes: downloading the upgrade file to a gateway via an IoT master station, and the gateway verifying the upgrade file and then broadcasting the verified upgrade file via a field area network.
[0011] In some embodiments, receiving the upgrade file of the sensor model includes: matching itself according to the preset content; if the sensor model and the hardware version are the same, but the software versions are different, then the upgrade file is saved according to the page number; otherwise, the upgrade file is discarded.
[0012] In some embodiments, after receiving the upgrade file of the sensor model, the method further includes: determining whether the overall file verification is correct, and if correct, waiting for the gateway to initiate the upgrade.
[0013] In some embodiments, the initialization function pointer is used to configure the analog-to-digital conversion pin and the calibration reference voltage; the data read function pointer is used to read the analog-to-digital conversion sample value; and the data parsing function pointer is used to convert the analog-to-digital conversion sample value into preset parameters.
[0014] In some embodiments, the monitoring section is used to monitor the preset parameters and sampling frequency; the parameter configuration section is used to configure the sampling frequency and filtering coefficient; and the alarm threshold management section is used to set at least one preset threshold.
[0015] In some embodiments, the method further includes: independently compiling instances of the sensor model into binary fragments and writing them into the specific memory segment via a serial port or BLE.
[0016] In some embodiments, the microcontroller is an 8-bit / 16-bit / 32-bit microcontroller.
[0017] In some embodiments, the method further includes: erasing the region of the microcontroller starting from the fixed address region; after writing the data in the upgrade file, resetting or directly reloading the sensor model pointer of the microcontroller.
[0018] Implementing one of the above-mentioned technical solutions of this application has the following advantages or beneficial effects: In this application, the dynamic update of the sensor model is achieved through a preset structure and fixed address region mapping. The sensor model is fixed to a fixed address region of the microcontroller via a linker script. The main program directly calls the sensor model interface through the fixed address, eliminating the need for recompilation or re-burning of the main control firmware. In this case, the sensor model update method for microcontrollers in this application uses a preset structure instead of a symbol table, encapsulating sensor-specific functions into substructures, which is more suitable for the sensor scenario. Simultaneously, the sensor model is more strictly isolated from the main program, and upgrades do not affect the main program logic. Furthermore, this application does not rely on a shared memory mechanism. By fixing the memory region through the linker script, symbol relocation is eliminated, resulting in extremely low resource overhead. Moreover, this application is designed for microcontrollers (without FPGA hardware partitioning), dividing the memory region through a software-level linker script, eliminating the need for a dedicated update control component and achieving updates only through simple address mapping. Additionally, this application eliminates the need for function block library management, directly calling the sensor model interface through a fixed address, eliminating the address correction process, and making it more lightweight. Furthermore, the sensor model update method for microcontrollers in this application does not require RTOS support and can run on bare-metal systems. Attached Figure Description
[0019] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. In the drawings:
[0020] Figure 1 This is a flowchart illustrating a sensor model update method for a microcontroller according to an embodiment of this application.
[0021] Figure 2 This is a schematic diagram illustrating a scenario of sensor model upgrade according to an embodiment of this application;
[0022] Figure 3 This is a schematic diagram of the memory layout according to an embodiment of this application;
[0023] Figure 4 This is a schematic diagram of the sensor model program replacement in an embodiment of this application. Detailed Implementation
[0024] To make the objectives, technical solutions, and advantages of this application clearer, various exemplary embodiments described below will be referenced to the accompanying drawings, which form part of the exemplary embodiments and depict various exemplary embodiments that may be adopted to implement this application. Unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this disclosure. It should be understood that they are merely examples of processes, methods, and apparatuses consistent with some aspects of this application disclosed as detailed in the appended claims, and other embodiments may be used, or structural and functional modifications may be made to the embodiments listed herein without departing from the scope and spirit of this application.
[0025] In the description of this application, it should be understood that the terms "center," "longitudinal," "lateral," etc., indicate the orientation or positional relationship based on the accompanying drawings, and are only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the referred element must have a specific orientation, or be constructed and operated in a specific orientation. The terms "first," "second," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. The term "multiple" means two or more. The terms "connected" and "linked" should be interpreted broadly, for example, they can be fixed connections, detachable connections, integral connections, mechanical connections, electrical connections, communication connections, direct connections, indirect connections through an intermediate medium, and can be the internal connection of two elements or the interaction relationship between two elements. The term "and / or" includes any and all combinations of one or more of the related listed items. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.
[0026] To illustrate the technical solutions described in this application, specific embodiments are provided below, showing only the parts related to the embodiments of this application.
[0027] like Figures 1 to 4 As shown, this application provides a sensor model update method suitable for microcontrollers, including the following steps (steps S1 to S3):
[0028] S1. Set up sensor models according to the preset structure, and bind each sensor model instance to a specific memory segment of the microcontroller through the compiler. The preset structure includes the basic function pointer and substructure of the sensor model. The basic function pointer includes at least the initialization function pointer, the data reading function pointer, and the data parsing function pointer. The substructure includes at least the monitoring part, the parameter configuration part, and the alarm threshold management part.
[0029] Specifically, initialization function pointers can be used to prepare the sensor for communication, configure the sensor's operating state, and put the sensor into a normal data acquisition mode. For example, initialization function pointers can be used to initialize the hardware interface for communication with the sensor, check the presence of the sensor on the bus, or initialize some parameters of the sensor. Data read function pointers can be used to obtain raw data from the sensor's registers or output interface according to a specified communication protocol; for example, raw data can be read by accessing a register address. Data parsing function pointers can convert the raw data obtained from the data read function pointer into engineering unit values, such as temperature values.
[0030] The substructure can be a sensor-specific functional substructure. The monitoring section can set the number of monitorable parameters and the list of monitorable parameter types; the parameter configuration section can set the number of configurable parameters; and the alarm threshold management section can set upper and lower thresholds to trigger alarms.
[0031] The following is a partial code snippet of the preset structure in an embodiment of this application:
[0032]
[0033]
[0034] In some embodiments, the initialization function pointer can be used to configure the analog-to-digital conversion pins and calibrate the reference voltage; the data read function pointer can be used to read the analog-to-digital conversion sampled values; and the data parsing function pointer is used to convert the analog-to-digital conversion sampled values into preset parameters. The analog-to-digital conversion sampled values are the raw data, and the preset parameters are engineering unit values.
[0035] In some embodiments, the monitoring section can be used to monitor preset parameters and sampling frequency; the parameter configuration section can be used to configure the sampling frequency and filtering coefficients; and the alarm threshold management section can be used to set at least one preset threshold. The preset threshold includes at least an upper limit threshold and a lower limit threshold, used to trigger an alarm.
[0036] Taking an NTC temperature sensor as an example, the following is a partial code snippet of the sensor model instance in this application being bound to a specific memory segment of the microcontroller:
[0037]
[0038]
[0039] The .sensor_model segment is a specific memory segment.
[0040] S2. The sensor model code and data corresponding to a specific memory segment are mapped to a fixed address area of the microcontroller using a linker script. The microcontroller's main program directly accesses the sensor model interface through this fixed address area. Mapping the model code and data to a fixed address area of the microcontroller, such as 0x08004000, using a custom linker script eliminates the need for address correction or relocation.
[0041] The following is a code snippet mapped to a fixed address region of the microcontroller:
[0042]
[0043] The following is a code snippet that calls the sensor model:
[0044] extern sensor_model_t*__sensor_model_start; / / Starting address of the linked script
[0045] sensor_model_t*current_sensor=(sensor_model_t*)__sensor_model_start;
[0046] / / Call the model function (direct address access, no intermediate layer)
[0047] current_sensor->Init();
[0048] current_sensor->read();
[0049] The main program can directly access the sensor model interface through the starting address without the need for a symbol table or dynamic parsing.
[0050] S3. Receive the upgrade file of the sensor model, erase a specific memory segment, copy the data of the upgrade file to the specific memory segment, and call the upgraded sensor model after the microcontroller restarts or reloads to complete the sensor model update.
[0051] In some embodiments, a sensor model update method applicable to a microcontroller may include: compiling the source file corresponding to each sensor model to generate a target file; splitting the target file into several pages according to a preset number of bytes; adding preset content to each page; merging all pages into a .dat file according to the page number order to obtain an upgrade file, wherein the preset content includes the total number of pages, the current page number, the total file verification, the current page verification, the sensor model, the hardware version number, the software version number, and the file content.
[0052] Specifically, the source file can be an .o file. For example, the source file for a temperature sensor can be sensor_model_temp.o, the source file for a humidity sensor can be sensor_model_hum.o, the source file for a smoke sensor can be sensor_model_smoke.o, and the source file for a liquid level sensor can be sensor_model_liquid.o. The default byte count can be 128 bytes. Furthermore, the total number of pages can be 2 bytes, the current page number can be 2 bytes, the total file checksum can be 2 bytes, the current page checksum can be 2 bytes, the sensor model can be 2 bytes, the hardware version number can be 2 bytes, the software version number can be 3 bytes, and the file content can be 128 bytes. The file can be split into pages according to a preset number of bytes, and preset content can be added to the beginning of each page. The resulting file can be merged into a single .dat file. For example, the upgrade file for a temperature sensor can be sensor_model_temp.dat, the upgrade file for a humidity sensor can be sensor_model_hum.dat, the upgrade file for a smoke sensor can be sensor_model_smoke.dat, and the upgrade file for a liquid level sensor can be sensor_model_liquid.dat.
[0053] In some embodiments, a sensor model update method applicable to a microcontroller may include: downloading an upgrade file to a gateway via an IoT master station; the gateway verifying the upgrade file and then broadcasting the verified upgrade file via a field area network. Specifically, if the gateway verifies the upgrade file and finds it correct, it can create an upgrade task for the sensor model and read the file content of the upgrade file page by page.
[0054] In some embodiments, receiving an upgrade file for a sensor model may include: matching itself against preset content; if the sensor model and hardware version are the same, but the software versions are different, then saving the upgrade file according to the page number; otherwise, discarding the upgrade file. Specifically, the upgrade file can be received in a field area network, and the sensor model, hardware version number, and software version number on each page of the upgrade file can be matched against its own sensor model, hardware version number, and software version number until all pages of data in the upgrade file have been received.
[0055] In some embodiments, after receiving the sensor model upgrade file, the sensor model update for the microcontroller may further include: determining whether the file verification is correct; if correct, waiting for the gateway to initiate the upgrade command. Specifically, the sensor model upgrade task can be started according to the upgrade command. After receiving the upgrade command, a specific memory segment can be erased immediately, the upgrade file can be copied to the specific memory segment, and after copying, it can be read and compared. If the verification is correct, the sensor model upgrade is completed.
[0056] In some embodiments, the sensor model update method for microcontrollers may further include: independently compiling instances of the sensor model into binary fragments, and writing them to a specific memory segment via serial port or BLE. Specifically, the binary fragments can be bin files, and the upgrade tool can select the corresponding model bin file based on the sensor type.
[0057] In some embodiments, the sensor model update method for microcontrollers may include: erasing the region of the microcontroller starting from a fixed address area; after writing the data to the upgrade file, resetting the microcontroller or directly reloading the sensor model pointer. The main program can automatically call the upgraded sensor model via __sensor_model_start without restarting the system. The microcontroller can be 8-bit / 16-bit / 32-bit, requiring no hardware expansion and offering strong compatibility.
[0058] The sensor model update method for microcontrollers presented in this application can run on bare-metal systems without requiring an RTOS or file system. It relies solely on the microcontroller's basic memory operations and has no OS dependency. Furthermore, the sensor model instance interacts directly with the main program through a fixed address region, eliminating the overhead of function lookups and symbol resolution, thus minimizing runtime overhead. Additionally, the sensor model instance interacts with the main program through a sensor model interface, avoiding access to the main program's private resources and preventing system crashes during the upgrade process. Therefore, this application only needs to load the currently required sensor model, reducing Flash usage by more than 50%. Sensor models can be upgraded independently without rewriting the main program, reducing upgrade data volume by 80% and improving upgrade convenience.
[0059] This application eliminates the need for dynamic libraries (.so / .dll), as resource-constrained microcontrollers do not support dynamic linking, and dynamic library resolution incurs significant overhead. Furthermore, it avoids FPGA partitioning technology, as microcontrollers lack hardware partitioning capabilities; similar functionality is achieved through software-level memory segmentation. Additionally, it eliminates the need for symbol tables, which increase the sensor model size, and microcontroller compilers typically do not support complex symbol resolution. Moreover, it eliminates the need for task scheduling updates, as resource-constrained scenarios do not require multi-task scheduling, and direct address access is more efficient. In summary, this application achieves dynamic updates of the sensor model in resource-constrained microcontroller scenarios, effectively avoiding the technical routes employed in industrial controllers, FPGAs, and SensorHubs.
[0060] In this application, the sensor model is dynamically updated through a preset structure and fixed address region mapping. The linker script fixes the sensor model to a fixed address region of the microcontroller, allowing the main program to directly call the sensor model interface via the fixed address, eliminating the need for recompilation or re-flashing of the main control firmware. In this approach, the sensor model update method for microcontrollers uses a preset structure instead of a symbol table, encapsulating sensor-specific functions into substructures, which better fits the sensor scenario. Simultaneously, the sensor model is more strictly isolated from the main program, ensuring that upgrades do not affect the main program logic. Furthermore, this application does not rely on shared memory mechanisms; it fixes the memory region through the linker script, eliminating the need for symbol relocation and resulting in extremely low resource overhead. Moreover, this application is designed for microcontrollers (without FPGA hardware partitioning), using software-level linker scripts to divide the memory region, eliminating the need for dedicated update control components and achieving updates solely through simple address mapping. Additionally, this application eliminates the need for function block library management, directly calling the sensor model interface via a fixed address, eliminating address correction processes and making it more lightweight. Finally, the sensor model update method for microcontrollers in this application does not require RTOS support and can run on bare-metal systems.
[0061] Those skilled in the art will understand that all or part of the features / steps of the above-described method embodiments can be implemented by methods, data processing systems, or computer programs. These features may be implemented without hardware, entirely in software, or in a combination of hardware and software. The aforementioned computer program may be stored in one or more computer-readable storage media. When the computer program is executed (e.g., by a processor), it performs the steps of the above-described sensor model update method embodiments suitable for microcontrollers.
[0062] The aforementioned storage media capable of storing program code include: static hard disks, solid-state hard disks, random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), optical storage devices, magnetic storage devices, flash memory, magnetic disks or optical disks and / or combinations thereof, that is, they can be implemented by any type of volatile or non-volatile storage devices or combinations thereof.
[0063] This application also provides a processing device embodiment, including one or more processors and a memory; wherein the memory is used to store one or more computer programs, and the one or more processors are used to execute the one or more computer programs stored in the memory, so that the processors perform the features / steps of the sensor model update method embodiment applicable to a microcontroller described above.
[0064] The above description is merely a preferred embodiment of this application. Those skilled in the art will understand that various changes or equivalent substitutions can be made to these features and embodiments without departing from the spirit and scope of this application. Furthermore, under the teachings of this application, these features and embodiments can be modified to adapt to specific situations and materials without departing from the spirit and scope of this application. Therefore, this application is not limited to the specific embodiments disclosed herein, and all embodiments falling within the scope of the claims of this application are within the protection scope of this application.
Claims
1. A sensor model updating method suitable for a single-chip microcomputer, characterized by, The method comprises the following steps: According to the preset structure, the sensor model is set, and the instance of each sensor model is bound to a specific memory segment of the single-chip microcomputer through a compiler, wherein the preset structure comprises a base function pointer and a sub-structure of the sensor model, and the base function pointer comprises at least an initialization function pointer, a data reading function pointer and a data analysis function pointer, and the sub-structure comprises at least a monitoring part, a parameter configuration part and an alarm threshold management part; The sensor model code and data corresponding to the specific memory segment are mapped to a fixed address region of the single-chip microcomputer through a link script, wherein the main program of the single-chip microcomputer directly accesses the sensor model interface through the fixed address region; An upgrade file of the sensor model is received, the specific memory segment is erased, the data of the upgrade file is copied into the specific memory segment, and the upgraded sensor model is called after the single-chip microcomputer is restarted or reloaded, so as to complete the update of the sensor model.
2. The sensor model updating method for a single-chip microcomputer according to claim 1, wherein The method comprises the following steps:
3. The sensor model updating method for a single-chip microcomputer according to claim 2, characterized by, The source file corresponding to each sensor model is compiled to generate a target file, the target file is split into a plurality of pages according to a preset byte number, preset content is added to each page, all pages are merged into a dat file according to the page sequence number, and the upgrade file is obtained, wherein the preset content comprises the total number of pages, the current page sequence number, the total file check, the current page check, the sensor model, the hardware version number, the software version number and the file content.
4. The sensor model updating method for a single-chip microcomputer according to claim 2, characterized by, The method comprises the following steps:
5. The method of claim 3, wherein the sensor model update for a microcontroller is performed by: The upgrade file of the sensor model is received, and the sensor model is matched according to the preset content; if the sensor model and the hardware version are the same, and the software version is different, the upgrade file is saved according to the page sequence number; otherwise, the upgrade file is discarded.
6. The method of claim 1, wherein the sensor model update for a microcontroller is performed by: After receiving the upgrade file of the sensor model, the method further comprises the following steps:
7. The method of claim 6, wherein the sensor model update is applied to a microcontroller. The initialization function pointer is used for configuring an analog-digital conversion pin and a calibration reference voltage; the data reading function pointer is used for reading an analog-digital conversion sample value; and the data analysis function pointer is used for converting the analog-digital conversion sample value into a preset parameter.
8. The method of claim 1, wherein the sensor model update for a microcontroller is performed by: The monitoring part is used for monitoring the preset parameter and a sampling frequency; the parameter configuration part is used for configuring the sampling frequency and a filter coefficient; and the alarm threshold management part is used for setting at least one preset threshold.
9. The method of claim 1, wherein the sensor model update for a microcontroller is performed by: The method further comprises the following steps:
10. The method of claim 1, wherein the sensor model update for a microcontroller is performed by: The single-chip microcomputer is an 8-bit / 16-bit / 32-bit single-chip microcomputer. The method further comprises the following steps: After the data of the upgrade file is written, the single-chip microcomputer is reset or directly reloaded with a sensor model pointer.