- Description
- Code Components
- Simulation Components
- Observations and Discussion
- Reproducing the Results
- Dependency Installation
- Program Flow: Simplified Version
- Toolstack
- References
- Help
- Contribution
- License
This project aims to implement the watchdog timer driver for the ATmega328p AVR and STM32f401xE microcontrollers and test its functionality using a blinking LED. The watchdog timer is a crucial safety feature available in many microcontrollers, designed to protect the system in case of a malfunction.
The watchdog timer operates like a countdown counter, starting from a specified value and decrementing towards zero. During normal operation, the system should reset the watchdog timer to its initial value at the end of each cycle. If the system fails to refresh the watchdog timer and it reaches zero, the watchdog timer will reset the system. This behavior indicates a potential malfunction that prevented the system from refreshing the watchdog timer.
The tests include a positive scenario where everything functions correctly and the system successfully refreshes the watchdog timer. Additionally, there are three negative scenarios discussed in the Observations and Discussion section, where the system fails to refresh the watchdog timer, triggering a reset to demonstrate the watchdog timer's protective mechanism.
This component includes two functions:
-
GPIO_Init: This function only configures the pin that the LED is connected to as output. -
GPIO_Write: This function takes the preferred state of the LED, either ON (1) or OFF (0), and depending on it, it sets the LED pin to high or low.
This component includes two functions:
-
LEDM_Init: This function initializes the LED pin and sets its initial state.- It calls
GPIO_Initto configure the LED pin as an output. - It sets the initial state of the LED to "High" by calling
GPIO_Write, which writes a 1 to the LED pin bit position in the port data register.
- It calls
-
LEDM_Manage: This function is intended to be called repeatedly inside the super loop of themainfunction inmain.c. It has two main responsibilities:- Toggling the LED state every 500ms.
- Calling
WDGM_AlivenessIndicationeach time it is executed to indicate the function's aliveness.
This component includes four functions:
-
WDGM_Init: This function initializes the internal variables of the Watchdog Manager, setting them to their initial values. -
WDGM_MainFunction: This function is called in the super loop of themainfunction inmain.c. Every 100ms, it checks the call rate of theLEDM_Managefunction to ensure it falls within a valid range. Ideally,LEDM_Manageshould be called 10 times per 100ms. Due to possible delays in hardware components, it accepts a range of 8 to 12 calls per 100ms. If the call rate is within this range, it sets the global variablewdgmStatustoOK, indicating normal operation. Otherwise, it setswdgmStatustoNOK, signaling a potential issue. -
WDGM_ProvideSupervisionStatus: This function returns the current status of the Watchdog Manager (wdgmStatus). It is used by theWDGDrvfile to determine if the system is functioning normally or if there is a malfunction requiring a system reset. -
WDGM_AlivenessIndication: This function is called byLEDM_Manageeach time it is executed. It increments a global counter that tracks the number of timesLEDM_Managehas been called within the 100ms period.
This component includes two main functions and one helper function that are needed for the Watchdog Driver:
-
WDGDrv_Init: This function initializes the watchdog timer.-
For ATmega328p AVR:
- Sets the maximum timeout value to 64ms.
- Activates the watchdog.
- Optionally initializes another timer, depending on your implementation. This can be included here or in a separate function or source file.
-
For STM32F401xE:
- Sets the maximum timeout value to 50ms.
- Disables the window mode.
- Enables the early interrupt feature.
- Activates the watchdog.
-
-
ISR(TIMER1_COMPA_vect): This is the Interrupt Service Routine (ISR) executed when Timer1 reaches the compare value (50ms). It calls theWDGDrv_IsrNotificationfunction to handle the watchdog functionality. -
WDGDrv_IsrNotification: This function retrieves the status of the Watchdog Manager usingWDGM_PovideSuppervisionStatus()and checks if the system is stuck in theWDGM_MainFunction. Based on this check:- If the status is
OKand the system is not stuck (stuck_flag != 1), it refreshes the watchdog timer. - If the system is stuck, no action is taken, which allows the watchdog timer to expire and potentially reset the system.
- If the status is
In this scenario, the system works perfectly normally and refreshes the watchdog at the correct time.
In this scenario, we should comment the call of the WDGM_MainFunction function in the main function. We expect the watchdog timer to reset the system each 64ms.
In this scenario, we should comment the call of the WDGM_AlivenessIndication function in the LEDM_Manage function. we expect the watchdog timer to reset the system after 114ms.
In this scenario, we should decrease the periodicity of the call of the LEDM_Manage to be every 5ms. we expect the watchdog timer to reset the system after 114ms.
- Install ATmega328p: Follow the ATmega328p installation guide in the Dependency Installation section.
- Import Project: Import the project from BlinkingLed_with_Watchdog into Eclipse IDE.
- Rebuild Project: Rebuild the project in Eclipse.
- Open Simulation: Open the simulation file in Proteus (preferably version 8.9).
- Load Program File: In Proteus, ensure the
.elfor.hexfile is loaded in theProgram Filefield by double-clicking the microcontroller. - Run Simulation: Start the simulation. By default, the code is set to the positive scenario.
To Reproduce Negative Scenarios:
- Option 1: Follow the above steps but open the specific scenario project in Eclipse. These projects are available here.
- Option 2: Edit the original project files to comment out the lines specified for each scenario. Rebuild the project in Eclipse and then run the simulation.
- Install the AVR toolchain from here. Note that you should select AVR 8-Bit Toolchain depending on the operating system you are using.
- Extract the downloaded zip folder to any preferable location.
- Install the AVR plugin to Eclipse by following these steps:
- From the
Helpmenu, openEclipse Marketplace. - Search for and install
AVR Eclipse Plugin.
- From the
- Before creating a project, remember to add the AVR paths to the system by following these steps.
- From the
Windowmenu, openPreferences. - From the right sidebar, open
AVRthenPaths. - Add the
AVR GCCpath by clicking the "Edit" button, changing the source to "Custom," and setting the "current value" to thebinfolder of the toolchain you downloaded. - Add the
AVR Header Filesby making the same steps, but the "current value" will be set to theincludefolder nested in theavrfolder of the toolchain. - Regarding the
GNU make, you should first download thexpack build toolsand then add the path to itsbinfolder in the "current value" field.
- From the
- The program starts by initializing the LED pin and setting it to ON.
- Each 10ms, the
LEDM_Manageis called. Inside this function,- The LED state only changes if 500ms has passed.
- We call the
WDGM_AlivenessIndicationto keep a count of how many times theLEDM_Managewas called.
- Returning to the
mainfunction, each 20ms, theWDGM_MainFunctionto make sure the system works fine and theLEDM_Manageis called at a normal rate (8-12 times per 100ms, ideally it should be 10 times per 100ms). - In parallel, if 50ms are passed, the
WDGDrv_IsrNotificationis called. This function checks if theLEDM_Manageis called at a correct rate and the program didn't get stuck at any phase (specifically in theWDGM_MainFunctionfunction). Depending on that check, either the system gets reset or the watchdog timer gets refreshed.
- Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet
- RM0368 Reference manual - STM32F401xB/C and STM32F401xD/E - advanced Arm®-based 32-bit MCUs
As mentioned, the shown results are generated for the ATmega328p microcontroller. Understanding pin configuration and reading chapters 10, 14, and 15 of the datasheet shall set you going with exploring the project files. However, if you encounter any issues or have questions, feel free to reach out.
Gratitude goes out to all team members for their valuable contributions to this project.
| Kareem Noureddine | Nada Omran | Nouran Khattab | Mohamed Sami |
This repo is under the MIT license. See LICENSE for details.
Made with ❤️ by Team 14 of the Embedded Systems Course - Systems & Biomedical Engineering, Cairo University (Class 2025).








