Skip to content

Commit ef1986a

Browse files
committed
feat: Bridge.begin waits to be greenlighted by MPU up to timeout
1 parent bf18094 commit ef1986a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/bridge.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define UPDATE_THREAD_PRIORITY 5
2323

2424
#define DEFAULT_SERIAL_BAUD 115200
25+
#define BEGIN_TIMEOUT_MS 5000
2526

2627
#include <zephyr/kernel.h>
2728
#include <zephyr/sys/atomic.h>
@@ -118,6 +119,8 @@ class BridgeClass {
118119
k_thread_stack_t *upd_stack_area{};
119120
struct k_thread upd_thread_data{};
120121

122+
static struct k_sem cleared_sem;
123+
121124
bool started = false;
122125

123126
public:
@@ -142,6 +145,7 @@ class BridgeClass {
142145
k_mutex_init(&read_mutex);
143146
k_mutex_init(&write_mutex);
144147
k_mutex_init(&bridge_mutex);
148+
k_sem_init(&cleared_sem, 0, 1); //
145149

146150
if (is_started()) return true;
147151

@@ -151,6 +155,9 @@ class BridgeClass {
151155
client = new RPCClient(*transport);
152156
server = new RPCServer(*transport);
153157

158+
// The service method greenLight is not registered to the MPU, but it is provided for signaling
159+
server->bind("greenLight", greenLight);
160+
154161
upd_stack_area = k_thread_stack_alloc(UPDATE_THREAD_STACK_SIZE, 0);
155162
upd_tid = k_thread_create(&upd_thread_data, upd_stack_area,
156163
UPDATE_THREAD_STACK_SIZE,
@@ -159,6 +166,10 @@ class BridgeClass {
159166
UPDATE_THREAD_PRIORITY, 0, K_NO_WAIT);
160167
k_thread_name_set(upd_tid, "bridge");
161168

169+
// This should not be mutexed and go before the call to RESET_METHOD
170+
// BEGIN_TIMEOUT_MS ensures compatibility with older versions that do not use signaling
171+
k_sem_take(&cleared_sem, K_MSEC(BEGIN_TIMEOUT_MS)); // wait to be cleared by the other side
172+
162173
k_mutex_lock(&bridge_mutex, K_FOREVER);
163174
bool res = false;
164175
started = call(RESET_METHOD).result(res) && res;
@@ -234,6 +245,10 @@ class BridgeClass {
234245

235246
private:
236247

248+
static void greenLight() {
249+
k_sem_give(&cleared_sem);
250+
}
251+
237252
void update_safe() {
238253

239254
// Lock read mutex

0 commit comments

Comments
 (0)