Quellcode durchsuchen

main: Wrap flash commands in critical section and enable them

It works! Unceremoniously bailing out on the WiFi driver without
shutting it down might not be great - handling the "GO" and "REBOOT"
commands gracefully in the main loop would probably be better.

But hey, it works!
Brian Starkey vor 1 Jahr
Ursprung
Commit
f0c0f516f5
2 geänderte Dateien mit 14 neuen und 3 gelöschten Zeilen
  1. 1
    0
      CMakeLists.txt
  2. 13
    3
      main.c

+ 1
- 0
CMakeLists.txt Datei anzeigen

@@ -27,4 +27,5 @@ target_link_libraries(picowota
27 27
 	hardware_structs
28 28
 	pico_cyw43_arch_lwip_poll
29 29
 	pico_stdlib
30
+	pico_sync
30 31
 )

+ 13
- 3
main.c Datei anzeigen

@@ -12,6 +12,7 @@
12 12
 
13 13
 #include "RP2040.h"
14 14
 #include "pico/time.h"
15
+#include "pico/critical_section.h"
15 16
 #include "hardware/dma.h"
16 17
 #include "hardware/flash.h"
17 18
 #include "hardware/structs/dma.h"
@@ -28,6 +29,7 @@
28 29
 
29 30
 extern const char *wifi_ssid;
30 31
 extern const char *wifi_pass;
32
+critical_section_t critical_section;
31 33
 
32 34
 #define BOOTLOADER_ENTRY_PIN 15
33 35
 #define BOOTLOADER_ENTRY_MAGIC 0xb105f00d
@@ -244,7 +246,9 @@ static uint32_t handle_erase(uint32_t *args_in, uint8_t *data_in, uint32_t *resp
244 246
 		return TCP_COMM_RSP_ERR;
245 247
 	}
246 248
 
249
+	critical_section_enter_blocking(&critical_section);
247 250
 	flash_range_erase(addr - XIP_BASE, size);
251
+	critical_section_exit(&critical_section);
248 252
 
249 253
 	return TCP_COMM_RSP_OK;
250 254
 }
@@ -291,7 +295,9 @@ static uint32_t handle_write(uint32_t *args_in, uint8_t *data_in, uint32_t *resp
291 295
 	uint32_t addr = args_in[0];
292 296
 	uint32_t size = args_in[1];
293 297
 
298
+	critical_section_enter_blocking(&critical_section);
294 299
 	flash_range_program(addr - XIP_BASE, data_in, size);
300
+	critical_section_exit(&critical_section);
295 301
 
296 302
 	resp_args_out[0] = calc_crc32((void *)addr, size);
297 303
 
@@ -359,8 +365,10 @@ static uint32_t handle_seal(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_
359 365
 		return TCP_COMM_RSP_ERR;
360 366
 	}
361 367
 
368
+	critical_section_enter_blocking(&critical_section);
362 369
 	flash_range_erase(IMAGE_HEADER_OFFSET, FLASH_SECTOR_SIZE);
363 370
 	flash_range_program(IMAGE_HEADER_OFFSET, (const uint8_t *)&hdr, sizeof(hdr));
371
+	critical_section_exit(&critical_section);
364 372
 
365 373
 	struct image_header *check = (struct image_header *)(XIP_BASE + IMAGE_HEADER_OFFSET);
366 374
 	if (memcmp(&hdr, check, sizeof(hdr))) {
@@ -522,6 +530,8 @@ int main()
522 530
 		printf("Connected.\n");
523 531
 	}
524 532
 
533
+	critical_section_init(&critical_section);
534
+
525 535
 	const struct comm_command *cmds[] = {
526 536
 		&sync_cmd,
527 537
 		&read_cmd,
@@ -529,10 +539,10 @@ int main()
529 539
 		&crc_cmd,
530 540
 		&erase_cmd,
531 541
 		&write_cmd,
532
-		//&seal_cmd,
533
-		//&go_cmd,
542
+		&seal_cmd,
543
+		&go_cmd,
534 544
 		&info_cmd,
535
-		//&reboot_cmd,
545
+		&reboot_cmd,
536 546
 	};
537 547
 
538 548
 	struct tcp_comm_ctx *tcp = tcp_comm_new(cmds, sizeof(cmds) / sizeof(cmds[0]), CMD_SYNC);

Laden…
Abbrechen
Speichern