Browse Source

Move reboot functionality to a separate library

This will be required for including into apps, so break it out.
Brian Starkey 2 years ago
parent
commit
ac21d218d6
5 changed files with 65 additions and 21 deletions
  1. 3
    0
      CMakeLists.txt
  2. 5
    21
      main.c
  3. 13
    0
      picowota_reboot/CMakeLists.txt
  4. 16
    0
      picowota_reboot/include/picowota/reboot.h
  5. 28
    0
      picowota_reboot/reboot.c

+ 3
- 0
CMakeLists.txt View File

20
 
20
 
21
 target_include_directories(picowota PRIVATE ${CMAKE_CURRENT_LIST_DIR})
21
 target_include_directories(picowota PRIVATE ${CMAKE_CURRENT_LIST_DIR})
22
 
22
 
23
+add_subdirectory(picowota_reboot)
24
+
23
 target_link_libraries(picowota
25
 target_link_libraries(picowota
24
 	cmsis_core
26
 	cmsis_core
25
 	hardware_dma
27
 	hardware_dma
30
 	pico_stdlib
32
 	pico_stdlib
31
 	pico_sync
33
 	pico_sync
32
 	pico_util
34
 	pico_util
35
+	picowota_reboot
33
 )
36
 )

+ 5
- 21
main.c View File

28
 
28
 
29
 #include "tcp_comm.h"
29
 #include "tcp_comm.h"
30
 
30
 
31
+#include "picowota/reboot.h"
32
+
31
 #ifdef DEBUG
33
 #ifdef DEBUG
32
 #include <stdio.h>
34
 #include <stdio.h>
33
 #include "pico/stdio_usb.h"
35
 #include "pico/stdio_usb.h"
64
 };
66
 };
65
 
67
 
66
 #define BOOTLOADER_ENTRY_PIN 15
68
 #define BOOTLOADER_ENTRY_PIN 15
67
-#define BOOTLOADER_ENTRY_MAGIC 0xb105f00d
68
 
69
 
69
 #define TCP_PORT 4242
70
 #define TCP_PORT 4242
70
 
71
 
501
 	.handle = &handle_info,
502
 	.handle = &handle_info,
502
 };
503
 };
503
 
504
 
504
-static void do_reboot(bool to_bootloader)
505
-{
506
-	hw_clear_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_ENABLE_BITS);
507
-	if (to_bootloader) {
508
-		watchdog_hw->scratch[5] = BOOTLOADER_ENTRY_MAGIC;
509
-		watchdog_hw->scratch[6] = ~BOOTLOADER_ENTRY_MAGIC;
510
-	} else {
511
-		watchdog_hw->scratch[5] = 0;
512
-		watchdog_hw->scratch[6] = 0;
513
-	}
514
-	watchdog_reboot(0, 0, 0);
515
-	while (1) {
516
-		tight_loop_contents();
517
-		asm("");
518
-	}
519
-}
520
-
521
 static uint32_t size_reboot(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
505
 static uint32_t size_reboot(uint32_t *args_in, uint32_t *data_len_out, uint32_t *resp_data_len_out)
522
 {
506
 {
523
 	*data_len_out = 0;
507
 	*data_len_out = 0;
554
 
538
 
555
 static bool should_stay_in_bootloader()
539
 static bool should_stay_in_bootloader()
556
 {
540
 {
557
-	bool wd_says_so = (watchdog_hw->scratch[5] == BOOTLOADER_ENTRY_MAGIC) &&
558
-		(watchdog_hw->scratch[6] == ~BOOTLOADER_ENTRY_MAGIC);
541
+	bool wd_says_so = (watchdog_hw->scratch[5] == PICOWOTA_BOOTLOADER_ENTRY_MAGIC) &&
542
+		(watchdog_hw->scratch[6] == ~PICOWOTA_BOOTLOADER_ENTRY_MAGIC);
559
 
543
 
560
 	return !gpio_get(BOOTLOADER_ENTRY_PIN) || wd_says_so;
544
 	return !gpio_get(BOOTLOADER_ENTRY_PIN) || wd_says_so;
561
 }
545
 }
633
 			case EVENT_TYPE_REBOOT:
617
 			case EVENT_TYPE_REBOOT:
634
 				tcp_comm_server_close(tcp);
618
 				tcp_comm_server_close(tcp);
635
 				cyw43_arch_deinit();
619
 				cyw43_arch_deinit();
636
-				do_reboot(ev.reboot.to_bootloader);
620
+				picowota_reboot(ev.reboot.to_bootloader);
637
 				/* Should never get here */
621
 				/* Should never get here */
638
 				break;
622
 				break;
639
 			case EVENT_TYPE_GO:
623
 			case EVENT_TYPE_GO:

+ 13
- 0
picowota_reboot/CMakeLists.txt View File

1
+add_library(picowota_reboot INTERFACE)
2
+
3
+target_include_directories(picowota_reboot INTERFACE
4
+	${CMAKE_CURRENT_LIST_DIR}/include
5
+)
6
+
7
+target_sources(picowota_reboot INTERFACE
8
+	${CMAKE_CURRENT_LIST_DIR}/reboot.c
9
+)
10
+
11
+target_link_libraries(picowota_reboot INTERFACE
12
+	hardware_structs
13
+)

+ 16
- 0
picowota_reboot/include/picowota/reboot.h View File

1
+/**
2
+ * Copyright (c) 2022 Brian Starkey <stark3y@gmail.com>
3
+ *
4
+ * SPDX-License-Identifier: BSD-3-Clause
5
+ */
6
+
7
+#ifndef __PICOWOTA_REBOOT_H__
8
+#define __PICOWOTA_REBOOT_H__
9
+
10
+#include <stdbool.h>
11
+
12
+#define PICOWOTA_BOOTLOADER_ENTRY_MAGIC 0xb105f00d
13
+
14
+void picowota_reboot(bool to_bootloader);
15
+
16
+#endif /* __PICOWOTA_REBOOT_H__ */

+ 28
- 0
picowota_reboot/reboot.c View File

1
+/**
2
+ * Copyright (c) 2022 Brian Starkey <stark3y@gmail.com>
3
+ *
4
+ * SPDX-License-Identifier: BSD-3-Clause
5
+ */
6
+
7
+#include "RP2040.h"
8
+#include "hardware/structs/watchdog.h"
9
+#include "hardware/watchdog.h"
10
+
11
+#include "picowota/reboot.h"
12
+
13
+void picowota_reboot(bool to_bootloader)
14
+{
15
+	hw_clear_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_ENABLE_BITS);
16
+	if (to_bootloader) {
17
+		watchdog_hw->scratch[5] = PICOWOTA_BOOTLOADER_ENTRY_MAGIC;
18
+		watchdog_hw->scratch[6] = ~PICOWOTA_BOOTLOADER_ENTRY_MAGIC;
19
+	} else {
20
+		watchdog_hw->scratch[5] = 0;
21
+		watchdog_hw->scratch[6] = 0;
22
+	}
23
+	watchdog_reboot(0, 0, 0);
24
+	while (1) {
25
+		tight_loop_contents();
26
+		asm("");
27
+	}
28
+}

Loading…
Cancel
Save