Skip to content

Commit 8c65615

Browse files
authored
Merge pull request #245 from brilliantlabsAR/watchdog
Watchdog
2 parents 7f76ee6 + 87d6eb0 commit 8c65615

File tree

10 files changed

+115
-5
lines changed

10 files changed

+115
-5
lines changed

source/application/Makefile

Lines changed: 2 additions & 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 \
@@ -89,6 +90,7 @@ C_FILES += \
8990
$(LIBRARIES)/nrfx/drivers/src/nrfx_spim.c \
9091
$(LIBRARIES)/nrfx/drivers/src/nrfx_systick.c \
9192
$(LIBRARIES)/nrfx/drivers/src/nrfx_twim.c \
93+
$(LIBRARIES)/nrfx/drivers/src/nrfx_wdt.c \
9294
$(LIBRARIES)/nrfx/helpers/nrfx_flag32_allocator.c \
9395
$(LIBRARIES)/nrfx/mdk/system_nrf52840.c \
9496
$(LIBRARIES)/segger/SEGGER_RTT.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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "lualib.h"
3232
#include "nrf_soc.h"
3333
#include "nrfx_log.h"
34+
#include "watchdog.h"
3435

3536
lua_State *L_global = NULL;
3637

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

5152
static void lua_break_signal_handler(lua_State *L, lua_Debug *ar)
5253
{
53-
lua_sethook(L, NULL, 0, 0);
54+
sethook_watchdog(L);
5455
luaL_error(L, "break signal");
5556
}
5657

@@ -72,6 +73,9 @@ void run_lua(bool factory_reset)
7273
error_with_message("Cannot create lua state: not enough memory");
7374
}
7475

76+
// Attach watchdog to hook
77+
sethook_watchdog(L);
78+
7579
// Open the standard libraries
7680
luaL_requiref(L, LUA_GNAME, luaopen_base, 1);
7781
luaL_requiref(L, LUA_COLIBNAME, luaopen_coroutine, 1);

source/application/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "nrfx_systick.h"
4444
#include "pinout.h"
4545
#include "spi.h"
46+
#include "watchdog.h"
4647

4748
bool not_real_hardware = false;
4849
bool stay_awake = false;
@@ -165,6 +166,11 @@ static void fpga_send_bitstream_bytes(void *context,
165166

166167
static void hardware_setup(bool *factory_reset)
167168
{
169+
// Configure watchdog
170+
{
171+
init_watchdog();
172+
}
173+
168174
// Configure systick so we can use it for simple delays
169175
{
170176
nrfx_systick_init();
@@ -403,6 +409,8 @@ int main(void)
403409

404410
while (1)
405411
{
412+
reload_watchdog(NULL, NULL);
413+
406414
run_lua(factory_reset);
407415

408416
factory_reset = false;

source/application/nrfx_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@
4747
#define NRFX_TWIM_ENABLED 1
4848
#define NRFX_TWIM0_ENABLED 1
4949

50+
#define NRFX_WDT_ENABLED 1
51+
5052
#include "templates/nrfx_config_nrf52840.h"

source/application/watchdog.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
.interrupt_priority = NRFX_WDT_DEFAULT_CONFIG_IRQ_PRIORITY,
36+
};
37+
38+
nrfx_wdt_channel_id watchdog_channel = NRF_WDT_RR0;
39+
40+
check_error(nrfx_wdt_init(&watchdog, &watchdog_config, NULL));
41+
check_error(nrfx_wdt_channel_alloc(&watchdog, &watchdog_channel));
42+
43+
nrfx_wdt_enable(&watchdog);
44+
nrfx_wdt_feed(&watchdog);
45+
}
46+
47+
void reload_watchdog(lua_State *L, lua_Debug *ar)
48+
{
49+
nrfx_wdt_channel_feed(&watchdog, NRF_WDT_RR0);
50+
}
51+
52+
void sethook_watchdog(lua_State *L)
53+
{
54+
lua_sethook(L, reload_watchdog, LUA_MASKCALL | LUA_MASKCOUNT, 2000);
55+
}

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);

source/error_logging.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,15 @@ void _check_error(nrfx_err_t error_code, const char *file, const int line)
146146
{
147147
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
148148
{
149-
LOG("Crashed at %s:%u - %s",
149+
LOG("Crashed at %s:%u - %s (0x%02x)",
150150
file,
151151
line,
152-
lookup_error_code(error_code));
152+
lookup_error_code(error_code),
153+
error_code);
154+
155+
for (size_t i = 0; i < 1000; i++)
156+
{
157+
}
153158

154159
__BKPT();
155160
}

source/nrfx_glue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define nrfx_gpiote_0_irq_handler GPIOTE_IRQHandler
3434
#define nrfx_rtc_1_irq_handler RTC1_IRQHandler
3535
#define nrfx_pdm_irq_handler PDM_IRQHandler
36+
#define nrfx_wdt_0_irq_handler WDT_IRQHandler
3637

3738
#define NRFX_ASSERT(expression) \
3839
do \

0 commit comments

Comments
 (0)