Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit bb3531d

Browse files
author
Bart Jonkers
committed
EM-290: create watchdog that monitors other micro-controllers connected via the serial protocol
1 parent c9fb00a commit bb3531d

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

stack/framework/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ FRAMEWORK_HEADER_DEFINE(BOOL FRAMEWORK_MODEM_INTERFACE_USE_DMA)
8585
SET(FRAMEWORK_MODEM_INTERFACE_USE_INTERRUPT_LINES "FALSE" CACHE BOOL "Enable interrupt lines to wake up modem and MCU for signaling serial communication")
8686
FRAMEWORK_HEADER_DEFINE(BOOL FRAMEWORK_MODEM_INTERFACE_USE_INTERRUPT_LINES)
8787

88+
SET(FRAMEWORK_SERIAL_PROTOCOL_WATCHDOG_LOG_ENABLED "FALSE" CACHE BOOL "Select whether to enable or disable the generation of logs from the serial protocol watchdog component")
89+
FRAMEWORK_HEADER_DEFINE(BOOL FRAMEWORK_SERIAL_PROTOCOL_WATCHDOG_LOG_ENABLED)
90+
8891
SET(FRAMEWORK_POWER_TRACKING_LOG_ENABLED "FALSE" CACHE BOOL "Select whether to enable or disable the generation of logs from the power tracking component")
8992
FRAMEWORK_HEADER_DEFINE(BOOL FRAMEWORK_POWER_TRACKING_LOG_ENABLED)
9093

stack/framework/components/serial_protocol/serial_protocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#endif
6363

6464
#ifdef FRAMEWORK_SERIAL_PROTOCOL_SUPPORT_INTERRUPT_LINES
65-
#define SERIAL_PROTOCOL_TIMEOUT (5 * TIMER_TICKS_PER_SEC)
65+
#define SERIAL_PROTOCOL_TIMEOUT (15 * TIMER_TICKS_PER_SEC) // Same as PING_TIMEOUT of serial protocol monitor, longer than the boot time of the other party
6666
#define SERIAL_PROTOCOL_TIMEOUT_BLOCK_TIME (5 * TIMER_TICKS_PER_MINUTE)
6767

6868
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[[
2+
Copyright (c) 2015-2021 University of Antwerp, Aloxy NV.
3+
4+
This file is part of Sub-IoT.
5+
See https://github.com/Sub-IoT/Sub-IoT-Stack for further info.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
]]
19+
20+
#Each Framework component must generate a single OBJECT library named
21+
#'${COMPONENT_LIBRARY_NAME}'
22+
ADD_LIBRARY(${COMPONENT_LIBRARY_NAME} OBJECT serial_protocol_watchdog.c)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "serial_protocol_watchdog.h"
2+
3+
#include "debug.h"
4+
#include "fifo.h"
5+
#include "log.h"
6+
#include "scheduler.h"
7+
8+
#if defined(FRAMEWORK_LOG_ENABLED) && defined(FRAMEWORK_SERIAL_PROTOCOL_WATCHDOG_LOG_ENABLED)
9+
#define DPRINT(...) log_print_string(__VA_ARGS__)
10+
#define DPRINT_DATA(...) log_print_data(__VA_ARGS__)
11+
#else
12+
#define DPRINT(...)
13+
#define DPRINT_DATA(...)
14+
#endif
15+
16+
#define PING_TIMEOUT (15 * TIMER_TICKS_PER_SEC) // Same as SERIAL_PROTOCOL_TIMEOUT, longer than the boot time of the other party
17+
#define PING_DATA 0xCC
18+
19+
typedef struct
20+
{
21+
serial_protocol_handle_t* sp_handle;
22+
reset_function_t reset_function;
23+
} priv_serial_protocol_watchdog_handle_t;
24+
25+
void timeout(void *arg)
26+
{
27+
assert(arg!=NULL);
28+
priv_serial_protocol_watchdog_handle_t* phandle = (priv_serial_protocol_watchdog_handle_t*)arg;
29+
DPRINT("No ping response received. Reset!!!");
30+
phandle->reset_function();
31+
}
32+
33+
void send_ping(void *arg)
34+
{
35+
assert(arg!=NULL);
36+
priv_serial_protocol_watchdog_handle_t* phandle = (priv_serial_protocol_watchdog_handle_t*)arg;
37+
timer_post_task_prio(&timeout, timer_get_counter_value() + PING_TIMEOUT, DEFAULT_PRIORITY, 0, arg);
38+
uint8_t data = PING_DATA;
39+
phandle->sp_handle->driver->serial_protocol_transfer_bytes(phandle->sp_handle, &data, 1, SERIAL_MESSAGE_TYPE_PING_REQUEST);
40+
}
41+
42+
void ping_response_handler(serial_protocol_handle_t* handle, serial_message_type_t type, fifo_t* cmd_fifo)
43+
{
44+
if(type != SERIAL_MESSAGE_TYPE_PING_RESPONSE)
45+
{
46+
return;
47+
}
48+
if(fifo_get_size(cmd_fifo) == 1)
49+
{
50+
uint8_t data;
51+
fifo_pop(cmd_fifo, &data, 1);
52+
if(data == PING_DATA)
53+
{
54+
timer_cancel_task(&timeout);
55+
}
56+
}
57+
}
58+
59+
void serial_protocol_watchdog_init(serial_protocol_watchdog_handle_t* spw_handle,
60+
serial_protocol_handle_t* sp_handle, reset_function_t reset_function, timer_tick_t check_interval)
61+
{
62+
assert(sp_handle != NULL);
63+
assert(spw_handle != NULL);
64+
assert(reset_function != NULL);
65+
assert(check_interval > PING_TIMEOUT);
66+
67+
priv_serial_protocol_watchdog_handle_t* phandle = (priv_serial_protocol_watchdog_handle_t*)spw_handle->priv_data;
68+
69+
phandle->sp_handle = sp_handle;
70+
phandle->reset_function = reset_function;
71+
72+
// The timer doesn't support multiple timers for the same function.
73+
// This functionality is needed if we want multiple instances of the serial_protocol_watchdog
74+
// Change the following register tasks to true if this functionality comes available
75+
sched_register_task_allow_multiple(send_ping, false);
76+
sched_register_task_allow_multiple(timeout, false);
77+
78+
sp_handle->driver->serial_protocol_register_handler(sp_handle, &ping_response_handler, SERIAL_MESSAGE_TYPE_PING_RESPONSE);
79+
80+
timer_post_task_prio(&send_ping, timer_get_counter_value() + check_interval, DEFAULT_PRIORITY, check_interval, phandle);
81+
}
82+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef SERIAL_PROTOCOL_WATCHDOG_H
2+
#define SERIAL_PROTOCOL_WATCHDOG_H
3+
4+
#include "serial_protocol.h"
5+
#include "timer.h"
6+
7+
typedef void (*reset_function_t)(void);
8+
9+
#define WATCHDOG_PRIV_BUF_SIZE (2*(sizeof(void *)/4))
10+
11+
typedef struct
12+
{
13+
uint32_t priv_data[WATCHDOG_PRIV_BUF_SIZE];
14+
}serial_protocol_watchdog_handle_t;
15+
16+
void serial_protocol_watchdog_init(serial_protocol_watchdog_handle_t* spw_handle,
17+
serial_protocol_handle_t* handle, reset_function_t reset_function, timer_tick_t check_interval);
18+
19+
void start(serial_protocol_watchdog_handle_t* spw_handle);
20+
void stop(serial_protocol_watchdog_handle_t* spw_handle);
21+
22+
#endif // SERIAL_PROTOCOL_WATCHDOG_H

0 commit comments

Comments
 (0)