rp2040.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2022 Stefan Kerkmann
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "quantum.h"
  4. #include "hal.h"
  5. #include "bootloader.h"
  6. #include "pico/bootrom.h"
  7. #if !defined(RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED)
  8. # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED_MASK 0U
  9. #else
  10. # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED_MASK (1U << RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED)
  11. #endif
  12. __attribute__((weak)) void mcu_reset(void) {
  13. NVIC_SystemReset();
  14. }
  15. void bootloader_jump(void) {
  16. reset_usb_boot(RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED_MASK, 0U);
  17. }
  18. void enter_bootloader_mode_if_requested(void) {}
  19. #if defined(RP2040_BOOTLOADER_DOUBLE_TAP_RESET)
  20. # if !defined(RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT)
  21. # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT 200U
  22. # endif
  23. // Needs to be located in a RAM section that is never initialized on boot to
  24. // preserve its value on reset
  25. static volatile uint32_t __attribute__((section(".ram0.bootloader_magic"))) magic_location;
  26. const uint32_t magic_token = 0xCAFEB0BA;
  27. // We can not use the __early_init / enter_bootloader_mode_if_requested hook as
  28. // we depend on an already initialized system with usable memory regions and
  29. // populated function pointer tables to the optimized math functions in the
  30. // bootrom. This function is called just prior to main.
  31. void __late_init(void) {
  32. // All clocks have to be enabled before jumping to the bootloader function,
  33. // otherwise the bootrom will be stuck infinitely.
  34. clocks_init();
  35. if (magic_location != magic_token) {
  36. magic_location = magic_token;
  37. // ChibiOS is not initialized at this point, so sleeping is only
  38. // possible via busy waiting. The internal timer peripheral is running
  39. // at this point with a precision of 1us.
  40. chSysPolledDelayX(MS2RTC(1 * MHZ, RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT));
  41. magic_location = 0;
  42. return;
  43. }
  44. magic_location = 0;
  45. reset_usb_boot(RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED_MASK, 0U);
  46. }
  47. #endif