Skip to content

Commit cd8ef79

Browse files
oganiglCopilot
andauthored
[FW-344] Watchdog (#501)
* fix watchdog to performance well * la pijadita de turno jajaajjaj * fix watchdog to performance well * la pijadita de turno jajaajjaj * Update Inc/HALAL/Services/Watchdog/Watchdog.hpp copilot leads Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Inc/HALAL/Services/Watchdog/Watchdog.hpp copilot leads II Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Src/HALAL/Services/Watchdog/Watchdog.cpp for the future checking the watchdog, please be careful with the dog Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent edae7a8 commit cd8ef79

5 files changed

Lines changed: 47 additions & 29 deletions

File tree

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#pragma once
2-
#include "HALAL/Models/PinModel/Pin.hpp"
32
#include "C++Utilities/CppUtils.hpp"
43
#include "ErrorHandler/ErrorHandler.hpp"
5-
4+
#include "HALAL/Models/PinModel/Pin.hpp"
65

76
extern IWDG_HandleTypeDef watchdog_handle;
8-
7+
extern bool reset_by_iwdg;
98

109
/**
1110
* @brief The watchdog class resets the board when it gets stuck inside a loop
1211
*
13-
* To ensure that the code its not stuck, the refresh function must be called at least once each period
14-
* Otherwise, the Watchdog will reset the board. The Watchdog its not active until it is started.
12+
* To ensure that the code is not stuck, the refresh function must be called at
13+
* least once every period; otherwise, the Watchdog will reset the board. The
14+
* Watchdog is not active until it is started.
1515
*/
16-
class Watchdog{
17-
public:
18-
19-
template<typename TimeUnit>
20-
static void start(chrono::duration<int64_t, TimeUnit> period){
21-
if((chrono::duration_cast<chrono::microseconds>(period)).count() > 32000000){
22-
ErrorHandler("Watchdog refresh interval is too big");
23-
}
24-
if((chrono::duration_cast<chrono::microseconds>(period)).count() < 125){
25-
ErrorHandler("Watchdog refresh interval is too short");
26-
}
27-
uint64_t microseconds = chrono::duration_cast<chrono::microseconds>(period).count();
28-
uint32_t RL = (uint64_t)((double) microseconds / 4 / 31.25) - 1;
16+
class Watchdog {
17+
public:
18+
static std::chrono::microseconds watchdog_time;
19+
20+
static void start() {
21+
if ((chrono::duration_cast<chrono::microseconds>(watchdog_time)).count() > 32000000) {
22+
ErrorHandler("Watchdog refresh interval is too big");
23+
}
24+
if ((chrono::duration_cast<chrono::microseconds>(watchdog_time)).count() < 125) {
25+
ErrorHandler("Watchdog refresh interval is too short");
26+
}
27+
uint64_t milliseconds = chrono::duration_cast<chrono::milliseconds>(watchdog_time).count();
28+
uint32_t RL = double(milliseconds) * 8.0 - 1; // this is the formula for the Reload
2929
uint32_t prescaler = 0;
30-
31-
while(RL > 4095){
32-
microseconds /= 2;
33-
prescaler += 1;
30+
while (RL > 4095) {
31+
milliseconds /= 2;
32+
prescaler += 1;
33+
RL = double(milliseconds) * 8.0 - 1;
3434
}
3535

3636
watchdog_handle.Instance = IWDG1;
@@ -40,8 +40,18 @@ class Watchdog{
4040
HAL_IWDG_Init(&watchdog_handle);
4141
}
4242

43-
static inline void refresh(){
44-
__HAL_IWDG_RELOAD_COUNTER(&watchdog_handle);
43+
static void refresh() {
44+
HAL_IWDG_Refresh(&watchdog_handle);
45+
}
46+
static void check_reset_flag(){
47+
if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDG1RST)) {
48+
reset_by_iwdg = true;
49+
50+
}
51+
__HAL_RCC_CLEAR_RESET_FLAGS();
52+
}
53+
template <typename TimeUnit>
54+
Watchdog(chrono::duration<int64_t, TimeUnit> period) {
55+
watchdog_time = std::chrono::duration_cast<std::chrono::microseconds>(period);
4556
}
46-
4757
};

Inc/ST-LIB.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "HALAL/HALAL.hpp" // Erase this when ST-LIB_LOW is finished.
44
#include "ST-LIB_LOW.hpp" // Erase this when ST-LIB_HIGH is finished
55
#include "ST-LIB_HIGH.hpp" // Highest layer up to the moment
6-
76
class STLIB {
87
public:
98

Src/HALAL/HALAL.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#ifndef SIM_ON
1111
void HALAL::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway,
1212
UART::Peripheral& printf_peripheral) {
13+
14+
#ifdef HAL_IWDG_MODULE_ENABLED
15+
Watchdog::check_reset_flag();
16+
Watchdog::start();
17+
#endif
1318
#if !defined STLIB_ETH
1419
#else
1520
Ethernet::inscribe();
@@ -67,7 +72,6 @@ void HALAL::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway,
6772
#else
6873
Ethernet::start(ip, subnet_mask, gateway);
6974
#endif
70-
7175
#ifdef HAL_TIM_MODULE_ENABLED
7276
Encoder::start();
7377
Global_RTC::start_rtc();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#include "HALAL/Services/Watchdog/Watchdog.hpp"
22

33
IWDG_HandleTypeDef watchdog_handle;
4+
std::chrono::microseconds Watchdog::watchdog_time = std::chrono::microseconds(1000000); //1 second by default
5+
bool reset_by_iwdg{};

Src/ST-LIB.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "ST-LIB.hpp"
1212

1313
void STLIB::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway, UART::Peripheral& printf_peripheral) {
14-
HALAL::start(ip, subnet_mask, gateway, printf_peripheral);
14+
HALAL::start(ip, subnet_mask, gateway, printf_peripheral);
1515
STLIB_LOW::start();
1616
STLIB_HIGH::start();
1717
}
@@ -22,11 +22,14 @@ void STLIB::start(string ip, string subnet_mask, string gateaway, UART::Periphe
2222

2323

2424
void STLIB::update() {
25+
#ifdef HAL_IWDG_MODULE_ENABLED
26+
Watchdog::refresh();
27+
#endif
2528
#if !defined STLIB_ETH
2629
#else
2730
Ethernet::update();
2831
Server::update_servers();
2932
#endif
30-
ErrorHandlerModel::ErrorHandlerUpdate();
33+
ErrorHandlerModel::ErrorHandlerUpdate();
3134

3235
}

0 commit comments

Comments
 (0)