Skip to content

Commit d5f16a3

Browse files
committed
Moved watchdog code to dedicated file
Setting watchdog hook from all hook changes
1 parent 945fd57 commit d5f16a3

File tree

8 files changed

+98
-25
lines changed

8 files changed

+98
-25
lines changed

source/application/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ C_FILES += \
3636
flash.c \
3737
luaport.c \
3838
spi.c \
39+
watchdog.c \
3940
lua_libraries/bluetooth.c \
4041
lua_libraries/camera.c \
4142
lua_libraries/display.c \

source/application/lua_libraries/bluetooth.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "lauxlib.h"
3131
#include "lua.h"
3232
#include "luaport.h"
33+
#include "watchdog.h"
3334

3435
static int lua_bluetooth_is_connected(lua_State *L)
3536
{
@@ -93,7 +94,7 @@ static struct lua_bluetooth_callback
9394

9495
static void lua_bluetooth_receive_callback_handler(lua_State *L, lua_Debug *ar)
9596
{
96-
lua_sethook(L, NULL, 0, 0);
97+
sethook_watchdog(L);
9798

9899
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_bluetooth_callback.function);
99100

source/application/lua_libraries/imu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "nrfx_gpiote.h"
3333
#include "nrfx_systick.h"
3434
#include "pinout.h"
35+
#include "watchdog.h"
3536

3637
#define PI 3.14159265
3738

@@ -55,7 +56,7 @@ static int lua_imu_callback_function = 0;
5556

5657
static void lua_imu_tap_callback_handler(lua_State *L, lua_Debug *ar)
5758
{
58-
lua_sethook(L, NULL, 0, 0);
59+
sethook_watchdog(L);
5960

6061
// Clear the interrupt by reading the status register
6162
check_error(i2c_read(ACCELEROMETER, 0x03, 0xFF).fail);

source/application/luaport.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
#include "lauxlib.h"
3030
#include "lua.h"
3131
#include "lualib.h"
32-
#include "main.h"
3332
#include "nrf_soc.h"
3433
#include "nrfx_log.h"
34+
#include "watchdog.h"
3535

3636
lua_State *L_global = NULL;
3737

@@ -51,7 +51,7 @@ void lua_write_to_repl(uint8_t *buffer, uint8_t length)
5151

5252
static void lua_break_signal_handler(lua_State *L, lua_Debug *ar)
5353
{
54-
lua_sethook(L, NULL, 0, 0);
54+
sethook_watchdog(L);
5555
luaL_error(L, "break signal");
5656
}
5757

@@ -73,7 +73,8 @@ void run_lua(bool factory_reset)
7373
error_with_message("Cannot create lua state: not enough memory");
7474
}
7575

76-
lua_sethook(L, reload_watchdog, LUA_MASKCOUNT, 100);
76+
// Attach watchdog to hook
77+
sethook_watchdog(L);
7778

7879
// Open the standard libraries
7980
luaL_requiref(L, LUA_GNAME, luaopen_base, 1);

source/application/main.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@
4141
#include "nrfx_log.h"
4242
#include "nrfx_rtc.h"
4343
#include "nrfx_systick.h"
44-
#include "nrfx_wdt.h"
4544
#include "pinout.h"
4645
#include "spi.h"
47-
48-
static nrfx_wdt_t watchdog = NRFX_WDT_INSTANCE(0);
46+
#include "watchdog.h"
4947

5048
bool not_real_hardware = false;
5149
bool stay_awake = false;
@@ -170,14 +168,7 @@ static void hardware_setup(bool *factory_reset)
170168
{
171169
// Configure watchdog
172170
{
173-
nrfx_wdt_config_t watchdog_config = NRFX_WDT_DEFAULT_CONFIG;
174-
nrfx_wdt_channel_id watchdog_channel = NRF_WDT_RR0;
175-
176-
check_error(nrfx_wdt_init(&watchdog, &watchdog_config, NULL));
177-
check_error(nrfx_wdt_channel_alloc(&watchdog, &watchdog_channel));
178-
179-
nrfx_wdt_enable(&watchdog);
180-
nrfx_wdt_feed(&watchdog);
171+
init_watchdog();
181172
}
182173

183174
// Configure systick so we can use it for simple delays
@@ -406,11 +397,6 @@ static void hardware_setup(bool *factory_reset)
406397
}
407398
}
408399

409-
void reload_watchdog(void)
410-
{
411-
nrfx_wdt_feed(&watchdog);
412-
}
413-
414400
int main(void)
415401
{
416402
LOG("Frame firmware " BUILD_VERSION " (" GIT_COMMIT ")");
@@ -421,10 +407,10 @@ int main(void)
421407

422408
bluetooth_setup(factory_reset);
423409

424-
nrfx_wdt_feed(&watchdog);
425-
426410
while (1)
427411
{
412+
reload_watchdog(NULL, NULL);
413+
428414
run_lua(factory_reset);
429415

430416
factory_reset = false;

source/application/main.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ extern bool not_real_hardware;
3030
extern bool stay_awake;
3131

3232
void shutdown(bool enable_imu_wakeup);
33-
34-
void reload_watchdog(void);

source/application/watchdog.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is a part of: https://github.com/brilliantlabsAR/frame-codebase
3+
*
4+
* Authored by: Raj Nakarja / Brilliant Labs Ltd. ([email protected])
5+
* Rohit Rathnam / Silicon Witchery AB ([email protected])
6+
* Uma S. Gupta / Techno Exponent ([email protected])
7+
*
8+
* ISC Licence
9+
*
10+
* Copyright © 2023 Brilliant Labs Ltd.
11+
*
12+
* Permission to use, copy, modify, and/or distribute this software for any
13+
* purpose with or without fee is hereby granted, provided that the above
14+
* copyright notice and this permission notice appear in all copies.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22+
* PERFORMANCE OF THIS SOFTWARE.
23+
*/
24+
25+
#include "lua.h"
26+
#include "nrfx_wdt.h"
27+
28+
static nrfx_wdt_t watchdog = NRFX_WDT_INSTANCE(0);
29+
30+
void init_watchdog(void)
31+
{
32+
nrfx_wdt_config_t watchdog_config = {
33+
.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_MASK,
34+
.reload_value = 6000,
35+
};
36+
37+
nrfx_wdt_channel_id watchdog_channel = NRF_WDT_RR0;
38+
39+
check_error(nrfx_wdt_init(&watchdog, &watchdog_config, NULL));
40+
check_error(nrfx_wdt_channel_alloc(&watchdog, &watchdog_channel));
41+
42+
nrfx_wdt_enable(&watchdog);
43+
nrfx_wdt_feed(&watchdog);
44+
}
45+
46+
void reload_watchdog(lua_State *L, lua_Debug *ar)
47+
{
48+
nrfx_wdt_channel_feed(&watchdog, NRF_WDT_RR0);
49+
}
50+
51+
void sethook_watchdog(lua_State *L)
52+
{
53+
lua_sethook(L, reload_watchdog, LUA_MASKCOUNT, 2000);
54+
}

source/application/watchdog.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is a part of: https://github.com/brilliantlabsAR/frame-codebase
3+
*
4+
* Authored by: Raj Nakarja / Brilliant Labs Ltd. ([email protected])
5+
* Rohit Rathnam / Silicon Witchery AB ([email protected])
6+
* Uma S. Gupta / Techno Exponent ([email protected])
7+
*
8+
* ISC Licence
9+
*
10+
* Copyright © 2023 Brilliant Labs Ltd.
11+
*
12+
* Permission to use, copy, modify, and/or distribute this software for any
13+
* purpose with or without fee is hereby granted, provided that the above
14+
* copyright notice and this permission notice appear in all copies.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22+
* PERFORMANCE OF THIS SOFTWARE.
23+
*/
24+
25+
#pragma once
26+
27+
#include "lua.h"
28+
29+
void init_watchdog(void);
30+
void reload_watchdog(lua_State *L, lua_Debug *ar);
31+
void sethook_watchdog(lua_State *L);

0 commit comments

Comments
 (0)