# Efficient Runtime Firmware Update Mechanism for LoRaWAN Class A Devices
## 3 Developed System
### Background--_Firmware Update During Runtime_  
The firmware update mechanism for remote devices, described in [17], allows firmware updates to be performed during program execution without interruptions or reboots by updating only specific segments of the code in the program memory. This method, in addition to minimizing or eliminating downtime, substantially reduces the amount of data transmitted during the update process, making it a reliable approach for updating remote LoRaWAN devices. Utilizing the non-volatile memory (NVM) controller present in most microcontrollers on the market, the code segments to be updated are modified in the program memory during the runtime of different ongoing applications without compromising their operation. The use of static and absolute addresses in various application functions to locate and isolate code segments during compilation, as proposed in [17], enables precise identification of each function, simplifying the process of updating code blocks. This approach significantly enhances code organization by avoiding substantial changes to the existing code and ensuring that small modifications in specific functions affect only the associated allocated memory areas (Figure 2). \begin{table}
\begin{tabular}{c c c c c c} \hline \hline \multirow{2}{*}{**SF**} & \multirow{2}{*}{**ToA [ms]**} & \multirow{2}{*}{**Data Rate**} & \multirow{2}{*}{**Sensitivity**} & \multirow{2}{*}{**Energy [J]**} & \multirow{2}{*}{**Max Payload**} \\  & & & [bps] & [dBm] & & **[bytes]** \\ \hline
12 & 2430 & 244.14 & \(-136.5\) & 1.389 & 51 \\
11 & 1310 & 447.59 & \(-134.0\) & 0.738 & 51 \\
10 & 706 & 813.80 & \(-131.5\) & 0.347 & 51 \\
9 & 338 & 1464.80 & \(-129.0\) & 0.184 & 115 \\
8 & 214 & 2604.10 & \(-126.5\) & 0.103 & 212 \\
7 & 119 & 4557.20 & \(-124.0\) & 0.057 & 212 \\ \hline \hline \end{tabular}  
* BW–125 kHz, Payload—51 bytes, CR 4/6, Header enable and CRC on. Credits: [[https://www.semtech.com/design-support/lora-calculator](https://www.semtech.com/design-support/lora-calculator)]([https://www.semtech.com/design-support/lora-calculator](https://www.semtech.com/design-support/lora-calculator)) (accessed on 9 June 2024).  
\end{table} 
Table 1: Spreading Factor (SF), Time on Air, (Adaptive) Data Rate, and Payload Size. This process requires a well-designed system architecture and a complete understanding of the program memory map to avoid overlaps between functions or application code blocks, making it necessary to reserve additional memory space adjacent to each function to be implemented. When compiling a firmware project using this method, after a minor bug correction or the inclusion of improved functionality, only the affected areas of program memory are reworked. This confines the altered code to a well-defined block of program memory, simplifying the update task. Consequently, only the changed code segment needs to be transmitted to the end device for the update. The update process (Figure 3) begins when the system receives a hexadecimal file in the Intel Hex format, containing the data related to the specific parts of the program code that have been updated. Upon receipt of the file or parts of the file, the system verifies the integrity and the completeness of the message. Once the integrity of the received file is verified, the system copies the file data to RAM, where it examines the received data to identify the code segments corresponding to each block of program memory, which is structured in rows. These segments are then mapped to the appropriate memory regions in the microcontroller based on the provided addresses. After identifying the code segments to be updated, the system reads the content of the corresponding program memory segments and stores them in the system's RAM for backup purposes. Next, the system updates the code segments with the data retrieved from the update Intel Hex file. In the next step, the system updates the program memory by overwriting each code segment with the updated version, replacing the current version in the microcontroller's flash memory.