Skip to content

Commit da47799

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

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/bridge.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define UPDATE_THREAD_PRIORITY 5
2323

2424
#define DEFAULT_SERIAL_BAUD 115200
25+
#define GREENLIGHT_METHOD "$/start"
26+
#define BEGIN_TIMEOUT_MS 5000
2527

2628
#include <zephyr/kernel.h>
2729
#include <zephyr/sys/atomic.h>
@@ -32,6 +34,13 @@
3234

3335
void updateEntryPoint(void *, void *, void *);
3436

37+
k_sem cleared_sem;
38+
39+
inline void greenLight() {
40+
Serial.println("Green Light");
41+
//k_sem_give(&cleared_sem);
42+
}
43+
3544
template<typename... Args>
3645
class RpcCall {
3746

@@ -179,17 +188,24 @@ class BridgeClass {
179188
k_mutex_init(&read_mutex);
180189
k_mutex_init(&write_mutex);
181190
k_mutex_init(&bridge_mutex);
191+
k_sem_init(&cleared_sem, 0, 1); //
182192

183193
if (is_started()) return true;
184194

185-
k_mutex_lock(&bridge_mutex, K_FOREVER);
186195

187196
serial_ptr->begin(baud);
188197
transport = new SerialTransport(*serial_ptr);
189198

190199
client = new RPCClient(*transport);
191200
server = new RPCServer(*transport);
192201

202+
// The service method greenLight is not registered to the MPU, but it is provided for signaling
203+
k_mutex_lock(&bridge_mutex, K_FOREVER);
204+
started = server->bind(GREENLIGHT_METHOD, greenLight);
205+
if (!started) Serial.println("Failed to bind greenlight");
206+
207+
k_mutex_unlock(&bridge_mutex);
208+
193209
upd_stack_area = k_thread_stack_alloc(UPDATE_THREAD_STACK_SIZE, 0);
194210
upd_tid = k_thread_create(&upd_thread_data, upd_stack_area,
195211
UPDATE_THREAD_STACK_SIZE,
@@ -198,6 +214,13 @@ class BridgeClass {
198214
UPDATE_THREAD_PRIORITY, 0, K_NO_WAIT);
199215
k_thread_name_set(upd_tid, "bridge");
200216

217+
// This should not be mutexed and go before the call to RESET_METHOD
218+
// BEGIN_TIMEOUT_MS ensures compatibility with older versions that do not use signaling
219+
Serial.println("Waiting to be cleared");
220+
k_sem_take(&cleared_sem, K_MSEC(BEGIN_TIMEOUT_MS)); // wait to be cleared by the other side
221+
Serial.println("Cleared->Resetting");
222+
223+
k_mutex_lock(&bridge_mutex, K_FOREVER);
201224
bool res = false;
202225
started = call(RESET_METHOD).result(res) && res;
203226
k_mutex_unlock(&bridge_mutex);
@@ -236,6 +259,13 @@ class BridgeClass {
236259

237260
k_mutex_unlock(&read_mutex);
238261

262+
Serial.print("Processing: ");
263+
for (size_t i = 0; i < req.size; i++) {
264+
Serial.print(" 0x");
265+
Serial.print(req.buffer[i], HEX);
266+
}
267+
Serial.println("");
268+
239269
server->process_request(req);
240270

241271
// Lock write mutex

0 commit comments

Comments
 (0)