MeterLogger
Data Structures | Functions | Variables
watchdog.c File Reference
#include <esp8266.h>
#include "driver/gpio16.h"
#include "watchdog.h"
#include "unix_time.h"
#include "wifi.h"
#include "config.h"
#include "led.h"
#include "debug.h"
Include dependency graph for watchdog.c:

Go to the source code of this file.

Data Structures

struct  watchdog_t
 

Functions

ICACHE_FLASH_ATTR static void ext_watchdog_timer_func (void *arg)
 
ICACHE_FLASH_ATTR static void wifi_reconnect_timer_func (void *arg)
 
ICACHE_FLASH_ATTR static void watchdog_timer_func (void *arg)
 
ICACHE_FLASH_ATTR void init_watchdog ()
 
ICACHE_FLASH_ATTR void start_watchdog ()
 
ICACHE_FLASH_ATTR void stop_watchdog ()
 
ICACHE_FLASH_ATTR bool add_watchdog (uint32_t id, watchdog_type_t type, uint32_t timeout)
 
ICACHE_FLASH_ATTR bool remove_watchdog (uint32_t id)
 
ICACHE_FLASH_ATTR void reset_watchdog (uint32_t id)
 

Variables

static os_timer_t watchdog_timer
 
static os_timer_t ext_watchdog_timer
 
static os_timer_t wifi_reconnect_timer
 
watchdog_t watchdog_list [WATCHDOG_MAX]
 
volatile uint8_t watchdog_list_len
 

Function Documentation

◆ add_watchdog()

ICACHE_FLASH_ATTR bool add_watchdog ( uint32_t  id,
watchdog_type_t  type,
uint32_t  timeout 
)

Definition at line 155 of file watchdog.c.

References get_uptime(), watchdog_t::id, watchdog_t::last_reset, os_printf, watchdog_t::timeout, watchdog_t::type, watchdog_list_len, and WATCHDOG_MAX.

155  {
157 #ifdef DEBUG
158  os_printf("add watchdog, id: %d\n", id);
159 #endif
165  return true;
166  }
167  else {
168 #ifdef DEBUG
169  os_printf("watchdog error, cant add more\n");
170 #endif
171  return false;
172  }
173 }
volatile uint32_t timeout
Definition: watchdog.c:18
volatile watchdog_type_t type
Definition: watchdog.c:17
volatile uint32_t id
Definition: watchdog.c:16
volatile uint32_t last_reset
Definition: watchdog.c:19
ICACHE_FLASH_ATTR uint32_t get_uptime(void)
Definition: unix_time.c:61
#define os_printf
Definition: osapi.h:62
volatile uint8_t watchdog_list_len
Definition: watchdog.c:23
watchdog_t watchdog_list[WATCHDOG_MAX]
Definition: watchdog.c:22
#define WATCHDOG_MAX
Definition: watchdog.h:1
Here is the call graph for this function:

◆ ext_watchdog_timer_func()

ICACHE_FLASH_ATTR static void ext_watchdog_timer_func ( void *  arg)
static

Definition at line 25 of file watchdog.c.

References gpio16_input_get(), and gpio16_output_set().

Referenced by start_watchdog().

25  {
26  if (gpio16_input_get()) {
28  }
29  else {
31  }
32 }
void ICACHE_FLASH_ATTR gpio16_output_set(uint8 value)
Definition: gpio16.c:19
uint8 ICACHE_FLASH_ATTR gpio16_input_get(void)
Definition: gpio16.c:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_watchdog()

ICACHE_FLASH_ATTR void init_watchdog ( )

Definition at line 125 of file watchdog.c.

References memset, and watchdog_list_len.

125  {
126  memset(watchdog_list, 0x00, sizeof(watchdog_list));
127  watchdog_list_len = 0;
128 }
#define memset(x, a, b)
Definition: platform.h:21
volatile uint8_t watchdog_list_len
Definition: watchdog.c:23
watchdog_t watchdog_list[WATCHDOG_MAX]
Definition: watchdog.c:22

◆ remove_watchdog()

ICACHE_FLASH_ATTR bool remove_watchdog ( uint32_t  id)

Definition at line 175 of file watchdog.c.

References watchdog_t::id, NOT_ENABLED, os_printf, watchdog_t::timeout, watchdog_t::type, and watchdog_list_len.

175  {
176  if (watchdog_list_len > 0) {
177 #ifdef DEBUG
178  os_printf("remove watchdog, id: %d\n", id);
179 #endif
184  return true;
185  }
186  else {
187 #ifdef DEBUG
188  os_printf("watchdog error, cant remove %d\n", id);
189 #endif
190  return false;
191  }
192 }
volatile uint32_t timeout
Definition: watchdog.c:18
volatile watchdog_type_t type
Definition: watchdog.c:17
volatile uint32_t id
Definition: watchdog.c:16
#define os_printf
Definition: osapi.h:62
volatile uint8_t watchdog_list_len
Definition: watchdog.c:23
watchdog_t watchdog_list[WATCHDOG_MAX]
Definition: watchdog.c:22

◆ reset_watchdog()

ICACHE_FLASH_ATTR void reset_watchdog ( uint32_t  id)

Definition at line 194 of file watchdog.c.

References get_uptime(), watchdog_t::last_reset, os_printf, and WATCHDOG_MAX.

194  {
195  uint32_t i;
196 
197 #ifdef DEBUG
198  os_printf("reset watchdog, id: %d\n", id);
199 #endif
200  for (i = 0; i < WATCHDOG_MAX; i++) {
201  if (watchdog_list[i].id == id) {
203  }
204  }
205 }
volatile uint32_t last_reset
Definition: watchdog.c:19
ICACHE_FLASH_ATTR uint32_t get_uptime(void)
Definition: unix_time.c:61
#define os_printf
Definition: osapi.h:62
watchdog_t watchdog_list[WATCHDOG_MAX]
Definition: watchdog.c:22
#define WATCHDOG_MAX
Definition: watchdog.h:1
Here is the call graph for this function:

◆ start_watchdog()

ICACHE_FLASH_ATTR void start_watchdog ( )

Definition at line 130 of file watchdog.c.

References ext_watchdog_timer, ext_watchdog_timer_func(), gpio16_output_conf(), gpio16_output_set(), NULL, os_printf, os_timer_arm, os_timer_disarm, os_timer_func_t, os_timer_setfn, watchdog_timer, and watchdog_timer_func().

130  {
131 #ifdef DEBUG
132  os_printf("watchdog started\n");
133 #endif
134  // start configurable software watchdog system
137  os_timer_arm(&watchdog_timer, 1000, 1);
138 
139  // start extern watchdog timer (MCP1316)
142  os_timer_arm(&ext_watchdog_timer, 1000, 1);
143  //Set GPIO16 to output mode
145  gpio16_output_set(1);
146 }
#define os_timer_disarm
Definition: osapi.h:51
ICACHE_FLASH_ATTR static void watchdog_timer_func(void *arg)
Definition: watchdog.c:72
ICACHE_FLASH_ATTR static void ext_watchdog_timer_func(void *arg)
Definition: watchdog.c:25
#define NULL
Definition: def.h:47
void ICACHE_FLASH_ATTR gpio16_output_conf(void)
Definition: gpio16.c:6
#define os_timer_func_t
Definition: os_type.h:35
#define os_printf
Definition: osapi.h:62
#define os_timer_setfn
Definition: osapi.h:52
void ICACHE_FLASH_ATTR gpio16_output_set(uint8 value)
Definition: gpio16.c:19
static os_timer_t watchdog_timer
Definition: watchdog.c:11
static os_timer_t ext_watchdog_timer
Definition: watchdog.c:12
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
Here is the call graph for this function:

◆ stop_watchdog()

ICACHE_FLASH_ATTR void stop_watchdog ( )

Definition at line 148 of file watchdog.c.

References ext_watchdog_timer, os_printf, and os_timer_disarm.

148  {
149 #ifdef DEBUG
150  os_printf("watchdog stopped\n");
151 #endif
153 }
#define os_timer_disarm
Definition: osapi.h:51
#define os_printf
Definition: osapi.h:62
static os_timer_t ext_watchdog_timer
Definition: watchdog.c:12

◆ watchdog_timer_func()

ICACHE_FLASH_ATTR static void watchdog_timer_func ( void *  arg)
static

Definition at line 72 of file watchdog.c.

References ext_watchdog_timer, get_uptime(), watchdog_t::last_reset, led_pattern_b(), NETWORK_RESTART, NETWORK_RESTART_DELAY, NOT_ENABLED, NULL, NULL_MODE, os_printf, os_timer_arm, os_timer_disarm, os_timer_func_t, os_timer_setfn, REBOOT, REBOOT_VIA_EXT_WD, set_my_auto_connect(), system_restart(), WATCHDOG_MAX, wifi_reconnect_timer, wifi_reconnect_timer_func(), wifi_set_opmode_current(), wifi_station_disconnect(), and wifi_stop_scan().

Referenced by start_watchdog().

72  {
73  uint32_t i;
74  uint32_t uptime;
75 
76  uptime = get_uptime();
77 // if (uptime) { // only run watchdog if we have unix time
78  for (i = 0; i < WATCHDOG_MAX; i++) {
79  if ((watchdog_list[i].type != NOT_ENABLED) &&
80  (watchdog_list[i].last_reset) &&
81  ((int32_t)watchdog_list[i].last_reset < (int32_t)(uptime - watchdog_list[i].timeout))) {
82 #ifdef DEBUG
83  os_printf("watchdog timeout, id: %d\n", watchdog_list[i].id);
84 #endif
85  switch (watchdog_list[i].type) {
86  case REBOOT:
88 #ifdef DEBUG
89  os_printf("reboot\n");
90 #endif
91  break;
92 
93  case NETWORK_RESTART:
94  // DEBUG: hack to get it to reconnect on weak wifi
95  // force reconnect to wireless
96  led_pattern_b(); // DEBUG to se if we ever try to restart network
98  set_my_auto_connect(false);
101 #ifdef DEBUG
102  os_printf("stopped wifi and wifi scanner\n");
103 #endif
104  // and (re)-connect when last wifi scan is done - wait 6 second before starting
108 #ifdef DEBUG
109  os_printf("scheduled wifi for restart...\n");
110 #endif
111  break;
112 
113  case REBOOT_VIA_EXT_WD:
115 #ifdef DEBUG
116  os_printf("reboot via ext watchdog\n");
117 #endif
118  break;
119  }
121  }
122  }
123 }
ICACHE_FLASH_ATTR void led_pattern_b(void)
Definition: led.c:113
void ICACHE_FLASH_ATTR wifi_stop_scan()
Definition: wifi.c:577
ICACHE_FLASH_ATTR static void wifi_reconnect_timer_func(void *arg)
Definition: watchdog.c:34
bool wifi_set_opmode_current(uint8 opmode)
#define os_timer_disarm
Definition: osapi.h:51
#define NULL_MODE
#define NULL
Definition: def.h:47
volatile uint32_t last_reset
Definition: watchdog.c:19
#define os_timer_func_t
Definition: os_type.h:35
void ICACHE_FLASH_ATTR set_my_auto_connect(bool enabled)
Definition: wifi.c:591
ICACHE_FLASH_ATTR uint32_t get_uptime(void)
Definition: unix_time.c:61
#define os_printf
Definition: osapi.h:62
#define os_timer_setfn
Definition: osapi.h:52
#define NETWORK_RESTART_DELAY
Definition: watchdog.h:6
void system_restart(void)
static os_timer_t ext_watchdog_timer
Definition: watchdog.c:12
watchdog_t watchdog_list[WATCHDOG_MAX]
Definition: watchdog.c:22
bool wifi_station_disconnect(void)
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
#define WATCHDOG_MAX
Definition: watchdog.h:1
static os_timer_t wifi_reconnect_timer
Definition: watchdog.c:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ wifi_reconnect_timer_func()

ICACHE_FLASH_ATTR static void wifi_reconnect_timer_func ( void *  arg)
static

Definition at line 34 of file watchdog.c.

References syscfg_t::ap_enabled, led_stop_pattern(), NULL, os_printf, os_timer_arm, os_timer_disarm, os_timer_func_t, os_timer_setfn, set_my_auto_connect(), STATION_MODE, STATIONAP_MODE, sys_cfg, wifi_get_opmode(), wifi_reconnect_timer, wifi_scan_is_running(), wifi_set_opmode_current(), wifi_start_scan(), and wifi_station_connect().

Referenced by watchdog_timer_func().

34  {
35  if (wifi_scan_is_running()) {
36  // reschedule if wifi scan is running
40 #ifdef DEBUG
41  os_printf("scanner still running - rescheduling reccont\n");
42 #endif
43  }
44  else {
46 #ifdef AP
47  if (sys_cfg.ap_enabled) {
50  }
51  }
52  else {
53  if (wifi_get_opmode() != STATION_MODE) {
55  }
56  }
57 #else
58  if (wifi_get_opmode() != STATION_MODE) {
60  }
61 #endif // AP
62  led_stop_pattern(); // DEBUG
63  set_my_auto_connect(true);
66 #ifdef DEBUG
67  os_printf("watchdog restarted wifi and started wifi scanner\n");
68 #endif
69  }
70 }
void ICACHE_FLASH_ATTR wifi_start_scan()
Definition: wifi.c:570
ICACHE_FLASH_ATTR static void wifi_reconnect_timer_func(void *arg)
Definition: watchdog.c:34
bool wifi_set_opmode_current(uint8 opmode)
#define os_timer_disarm
Definition: osapi.h:51
#define STATIONAP_MODE
#define NULL
Definition: def.h:47
#define os_timer_func_t
Definition: os_type.h:35
void ICACHE_FLASH_ATTR set_my_auto_connect(bool enabled)
Definition: wifi.c:591
#define os_printf
Definition: osapi.h:62
uint8 wifi_get_opmode(void)
#define os_timer_setfn
Definition: osapi.h:52
bool ap_enabled
Definition: config.h:51
bool ICACHE_FLASH_ATTR wifi_scan_is_running()
Definition: wifi.c:583
#define STATION_MODE
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
ICACHE_FLASH_ATTR void led_stop_pattern(void)
Definition: led.c:128
syscfg_t sys_cfg
Definition: config.c:12
bool wifi_station_connect(void)
static os_timer_t wifi_reconnect_timer
Definition: watchdog.c:13
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ext_watchdog_timer

os_timer_t ext_watchdog_timer
static

Definition at line 12 of file watchdog.c.

Referenced by start_watchdog(), stop_watchdog(), and watchdog_timer_func().

◆ watchdog_list

watchdog_t watchdog_list[WATCHDOG_MAX]

Definition at line 22 of file watchdog.c.

◆ watchdog_list_len

volatile uint8_t watchdog_list_len

Definition at line 23 of file watchdog.c.

Referenced by add_watchdog(), init_watchdog(), and remove_watchdog().

◆ watchdog_timer

os_timer_t watchdog_timer
static

Definition at line 11 of file watchdog.c.

Referenced by start_watchdog().

◆ wifi_reconnect_timer

os_timer_t wifi_reconnect_timer
static

Definition at line 13 of file watchdog.c.

Referenced by watchdog_timer_func(), and wifi_reconnect_timer_func().