Skip to content

Commit e09a687

Browse files
committed
example to work with async loading
1 parent a660b37 commit e09a687

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

examples/tests/app_loader/main.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
static bool setup_done = false; // to check if setup is done
2323
static bool write_done = false; // to check if writing to flash is done
24+
static bool load_done = false; // to check if the process was loaded successfully
2425

2526
/******************************************************************************************************
2627
* Callback functions
@@ -48,6 +49,14 @@ static void app_write_done_callback(__attribute__((unused)) int length,
4849
write_done = true;
4950
}
5051

52+
static void app_load_done_callback( __attribute__((unused)) int length,
53+
__attribute__((unused)) int arg1,
54+
__attribute__((unused)) int arg2,
55+
__attribute__((unused)) void *ud)
56+
{
57+
load_done = true;
58+
}
59+
5160
static void button_callback(int btn_num,
5261
int val,
5362
__attribute__ ((unused)) int arg2,
@@ -75,6 +84,7 @@ static void button_callback(int btn_num,
7584
printf("[Log] Exiting Application.\n");
7685
tock_exit(ret); // we failed, so we exit the program.
7786
} else {
87+
printf("[Log] Yielding for setup done.\n");
7888
yield_for(&setup_done); // wait until the padding app write is done before you send your app, or it will fail during write
7989
setup_done = false;
8090
printf("[Success] Setup successful. Attempting to write app to flash now.\n");
@@ -173,6 +183,7 @@ int write_app(double size, uint8_t binary[]){
173183
ret = app_loader_command_write(flash_offset, FLASH_BUFFER_SIZE);
174184
if (ret != 0) {
175185
printf("[Error] Failed writing data to flash at address: 0x%lx\n", flash_offset);
186+
printf("[Error] Error nature: %d\n", ret);
176187
return -1;
177188
}
178189
yield_for(&write_done); // wait until write done callback
@@ -202,26 +213,34 @@ int main(void) {
202213
button_enable_interrupt(i);
203214
}
204215

205-
// set up the write done and button press callbacks
216+
// set up the setup done callback
206217
int err1 = app_loader_setup_subscribe(app_setup_done_callback, NULL);
207218
if (err1 != 0) {
208219
printf("[Error] Failed to set setup done callback: %d\n", err1);
209220
return err1;
210221
}
211222

212-
// set up the write done and button press callbacks
223+
// set up the write done callback
213224
int err2 = app_loader_write_subscribe(app_write_done_callback, NULL);
214225
if (err2 != 0) {
215226
printf("[Error] Failed to set flash write done callback: %d\n", err2);
216227
return err2;
217228
}
218229

219-
int err3 = button_subscribe(button_callback, NULL);
230+
// set up the load done callback
231+
int err3 = app_loader_load_subscribe(app_load_done_callback, NULL);
220232
if (err3 != 0) {
221-
printf("[Error] Failed to set button callback: %d.\n", err3);
233+
printf("[Error] Failed to set load done callback: %d\n", err3);
222234
return err3;
223235
}
224236

237+
// setup the button press callback
238+
int err4 = button_subscribe(button_callback, NULL);
239+
if (err4 != 0) {
240+
printf("[Error] Failed to set button callback: %d.\n", err4);
241+
return err4;
242+
}
243+
225244
// Check if the app_loader driver exists.
226245
int ret;
227246
ret = app_loader_exists();

libtock/internal/app_loader.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ int app_loader_write_subscribe(subscribe_upcall cb, void *userdata) {
1616
return tock_subscribe_return_to_returncode(sval);
1717
}
1818

19+
int app_loader_load_subscribe(subscribe_upcall cb, void *userdata) {
20+
subscribe_return_t sval = subscribe(DRIVER_NUM_APP_LOADER, 2, cb, userdata);
21+
return tock_subscribe_return_to_returncode(sval);
22+
}
23+
1924
int app_loader_write_buffer(uint8_t *buffer, uint32_t len) {
2025
allow_ro_return_t aval = allow_readonly(DRIVER_NUM_APP_LOADER, 0, (void*) buffer, len);
2126
return tock_allow_ro_return_to_returncode(aval);

libtock/internal/app_loader.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ int app_loader_command_write(uint32_t flash_offset, uint32_t write_length);
5959
*/
6060
int app_loader_command_load(void);
6161

62+
/*
63+
* Function to setup the callback from capsule.
64+
* This function takes in the function that will be executed
65+
* when the callback is triggered.
66+
*/
67+
int app_loader_load_subscribe(subscribe_upcall cb, void *userdata);
68+
69+
6270
/*
6371
* Internal function to write new app to flash. Takes app size as argument.
6472
*/

0 commit comments

Comments
 (0)