target.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <target.h>
  2. #include <stm32wbxx.h>
  3. #include <stm32wbxx_ll_system.h>
  4. #include <stm32wbxx_ll_bus.h>
  5. #include <stm32wbxx_ll_utils.h>
  6. #include <stm32wbxx_ll_rcc.h>
  7. #include <stm32wbxx_ll_rtc.h>
  8. #include <stm32wbxx_ll_pwr.h>
  9. #include <stm32wbxx_ll_gpio.h>
  10. #include <stm32wbxx_hal_flash.h>
  11. #include <lib/toolbox/version.h>
  12. #include <api-hal.h>
  13. // Boot request enum
  14. #define BOOT_REQUEST_TAINTED 0x00000000
  15. #define BOOT_REQUEST_CLEAN 0xDADEDADE
  16. #define BOOT_REQUEST_DFU 0xDF00B000
  17. // Boot to DFU pin
  18. #define BOOT_DFU_PORT GPIOB
  19. #define BOOT_DFU_PIN LL_GPIO_PIN_11
  20. // USB pins
  21. #define BOOT_USB_PORT GPIOA
  22. #define BOOT_USB_DM_PIN LL_GPIO_PIN_11
  23. #define BOOT_USB_DP_PIN LL_GPIO_PIN_12
  24. #define BOOT_USB_PIN (BOOT_USB_DM_PIN | BOOT_USB_DP_PIN)
  25. #define RTC_CLOCK_IS_READY() (LL_RCC_LSE_IsReady() && LL_RCC_LSI1_IsReady())
  26. void target_led_control(char* c) {
  27. api_hal_light_set(LightRed, 0x00);
  28. api_hal_light_set(LightGreen, 0x00);
  29. api_hal_light_set(LightBlue, 0x00);
  30. do {
  31. if(*c == 'R') {
  32. api_hal_light_set(LightRed, 0xFF);
  33. } else if(*c == 'G') {
  34. api_hal_light_set(LightGreen, 0xFF);
  35. } else if(*c == 'B') {
  36. api_hal_light_set(LightBlue, 0xFF);
  37. } else if(*c == '.') {
  38. LL_mDelay(125);
  39. api_hal_light_set(LightRed, 0x00);
  40. api_hal_light_set(LightGreen, 0x00);
  41. api_hal_light_set(LightBlue, 0x00);
  42. LL_mDelay(125);
  43. } else if(*c == '-') {
  44. LL_mDelay(250);
  45. api_hal_light_set(LightRed, 0x00);
  46. api_hal_light_set(LightGreen, 0x00);
  47. api_hal_light_set(LightBlue, 0x00);
  48. LL_mDelay(250);
  49. } else if(*c == '|') {
  50. api_hal_light_set(LightRed, 0x00);
  51. api_hal_light_set(LightGreen, 0x00);
  52. api_hal_light_set(LightBlue, 0x00);
  53. }
  54. c++;
  55. } while(*c != 0);
  56. }
  57. void clock_init() {
  58. LL_Init1msTick(4000000);
  59. LL_SetSystemCoreClock(4000000);
  60. }
  61. void gpio_init() {
  62. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  63. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  64. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
  65. // USB D+
  66. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_MODE_OUTPUT);
  67. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  68. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  69. // USB D-
  70. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_MODE_OUTPUT);
  71. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  72. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  73. // Button: back
  74. LL_GPIO_SetPinMode(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_MODE_INPUT);
  75. LL_GPIO_SetPinPull(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_PULL_UP);
  76. }
  77. void rtc_init() {
  78. // LSE and RTC
  79. LL_PWR_EnableBkUpAccess();
  80. if(!RTC_CLOCK_IS_READY()) {
  81. // Start LSI1 needed for CSS
  82. LL_RCC_LSI1_Enable();
  83. // Try to start LSE normal way
  84. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_MEDIUMLOW);
  85. LL_RCC_LSE_Enable();
  86. uint32_t c = 0;
  87. while(!RTC_CLOCK_IS_READY() && c < 200) {
  88. LL_mDelay(10);
  89. c++;
  90. }
  91. // Plan B: reset backup domain
  92. if(!RTC_CLOCK_IS_READY()) {
  93. target_led_control("-R.R.R.");
  94. LL_RCC_ForceBackupDomainReset();
  95. LL_RCC_ReleaseBackupDomainReset();
  96. NVIC_SystemReset();
  97. }
  98. // Set RTC domain clock to LSE
  99. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  100. // Enable LSE CSS
  101. LL_RCC_LSE_EnableCSS();
  102. }
  103. // Enable clocking
  104. LL_RCC_EnableRTC();
  105. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  106. }
  107. void version_save(void) {
  108. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR1, (uint32_t)version_get());
  109. }
  110. void usb_wire_reset() {
  111. LL_GPIO_ResetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  112. LL_mDelay(10);
  113. LL_GPIO_SetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  114. }
  115. void target_init() {
  116. clock_init();
  117. gpio_init();
  118. api_hal_init();
  119. target_led_control("RGB");
  120. rtc_init();
  121. version_save();
  122. usb_wire_reset();
  123. // Errata 2.2.9, Flash OPTVERR flag is always set after system reset
  124. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  125. }
  126. int target_is_dfu_requested() {
  127. if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) {
  128. // Default system state is tainted
  129. // We must ensure that MCU is cleanly booted
  130. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
  131. NVIC_SystemReset();
  132. } else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
  133. return 1;
  134. }
  135. LL_mDelay(100);
  136. if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {
  137. return 1;
  138. }
  139. return 0;
  140. }
  141. void target_switch(void* offset) {
  142. asm volatile("ldr r3, [%0] \n"
  143. "msr msp, r3 \n"
  144. "ldr r3, [%1] \n"
  145. "mov pc, r3 \n"
  146. :
  147. : "r"(offset), "r"(offset + 0x4)
  148. : "r3");
  149. }
  150. void target_switch2dfu() {
  151. target_led_control("B");
  152. // Mark system as tainted, it will be soon
  153. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
  154. // Remap memory to system bootloader
  155. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SYSTEMFLASH);
  156. target_switch(0x0);
  157. }
  158. void target_switch2os() {
  159. target_led_control("G");
  160. SCB->VTOR = OS_OFFSET;
  161. target_switch((void*)(BOOT_ADDRESS + OS_OFFSET));
  162. }