Browse Source

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 2 years ago
parent
commit
f0c0f516f5
2 changed files with 14 additions and 3 deletions
  1. 1
    0
      CMakeLists.txt
  2. 13
    3
      main.c

+ 1
- 0
CMakeLists.txt View File

27
 	hardware_structs
27
 	hardware_structs
28
 	pico_cyw43_arch_lwip_poll
28
 	pico_cyw43_arch_lwip_poll
29
 	pico_stdlib
29
 	pico_stdlib
30
+	pico_sync
30
 )
31
 )

+ 13
- 3
main.c View File

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

Loading…
Cancel
Save