target.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <furi-hal.h>
  13. #include <u8g2.h>
  14. const uint8_t I_Warning_30x23_0[] = {
  15. 0x00, 0xC0, 0x00, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0xF0, 0x03, 0x00, 0x00, 0xF0, 0x03, 0x00,
  16. 0x00, 0xF8, 0x07, 0x00, 0x00, 0x3C, 0x0F, 0x00, 0x00, 0x3C, 0x0F, 0x00, 0x00, 0x3E, 0x1F, 0x00,
  17. 0x00, 0x3F, 0x3F, 0x00, 0x00, 0x3F, 0x3F, 0x00, 0x80, 0x3F, 0x7F, 0x00, 0xC0, 0x3F, 0xFF, 0x00,
  18. 0xC0, 0x3F, 0xFF, 0x00, 0xE0, 0x3F, 0xFF, 0x01, 0xF0, 0x3F, 0xFF, 0x03, 0xF0, 0x3F, 0xFF, 0x03,
  19. 0xF8, 0x3F, 0xFF, 0x07, 0xFC, 0xFF, 0xFF, 0x0F, 0xFC, 0xFF, 0xFF, 0x0F, 0xFE, 0x3F, 0xFF, 0x1F,
  20. 0xFF, 0x3F, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0x3F, 0xFE, 0xFF, 0xFF, 0x1F,
  21. };
  22. // Boot request enum
  23. #define BOOT_REQUEST_TAINTED 0x00000000
  24. #define BOOT_REQUEST_CLEAN 0xDADEDADE
  25. #define BOOT_REQUEST_DFU 0xDF00B000
  26. // Boot to DFU pin
  27. #define BOOT_DFU_PORT GPIOB
  28. #define BOOT_DFU_PIN LL_GPIO_PIN_11
  29. // USB pins
  30. #define BOOT_USB_PORT GPIOA
  31. #define BOOT_USB_DM_PIN LL_GPIO_PIN_11
  32. #define BOOT_USB_DP_PIN LL_GPIO_PIN_12
  33. #define BOOT_USB_PIN (BOOT_USB_DM_PIN | BOOT_USB_DP_PIN)
  34. #define RTC_CLOCK_IS_READY() (LL_RCC_LSE_IsReady() && LL_RCC_LSI1_IsReady())
  35. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
  36. uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
  37. void target_led_control(char* c) {
  38. furi_hal_light_set(LightRed, 0x00);
  39. furi_hal_light_set(LightGreen, 0x00);
  40. furi_hal_light_set(LightBlue, 0x00);
  41. do {
  42. if(*c == 'R') {
  43. furi_hal_light_set(LightRed, 0xFF);
  44. } else if(*c == 'G') {
  45. furi_hal_light_set(LightGreen, 0xFF);
  46. } else if(*c == 'B') {
  47. furi_hal_light_set(LightBlue, 0xFF);
  48. } else if(*c == '.') {
  49. LL_mDelay(125);
  50. furi_hal_light_set(LightRed, 0x00);
  51. furi_hal_light_set(LightGreen, 0x00);
  52. furi_hal_light_set(LightBlue, 0x00);
  53. LL_mDelay(125);
  54. } else if(*c == '-') {
  55. LL_mDelay(250);
  56. furi_hal_light_set(LightRed, 0x00);
  57. furi_hal_light_set(LightGreen, 0x00);
  58. furi_hal_light_set(LightBlue, 0x00);
  59. LL_mDelay(250);
  60. } else if(*c == '|') {
  61. furi_hal_light_set(LightRed, 0x00);
  62. furi_hal_light_set(LightGreen, 0x00);
  63. furi_hal_light_set(LightBlue, 0x00);
  64. }
  65. c++;
  66. } while(*c != 0);
  67. }
  68. void target_clock_init() {
  69. LL_Init1msTick(4000000);
  70. LL_SetSystemCoreClock(4000000);
  71. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  72. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  73. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
  74. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOD);
  75. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOE);
  76. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOH);
  77. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  78. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
  79. }
  80. void target_gpio_init() {
  81. // USB D+
  82. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_MODE_OUTPUT);
  83. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  84. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  85. // USB D-
  86. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_MODE_OUTPUT);
  87. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  88. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  89. // Button: back
  90. LL_GPIO_SetPinMode(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_MODE_INPUT);
  91. LL_GPIO_SetPinPull(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_PULL_UP);
  92. }
  93. void target_rtc_init() {
  94. // LSE and RTC
  95. LL_PWR_EnableBkUpAccess();
  96. if(!RTC_CLOCK_IS_READY()) {
  97. // Start LSI1 needed for CSS
  98. LL_RCC_LSI1_Enable();
  99. // Try to start LSE normal way
  100. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_HIGH);
  101. LL_RCC_LSE_Enable();
  102. uint32_t c = 0;
  103. while(!RTC_CLOCK_IS_READY() && c < 200) {
  104. LL_mDelay(10);
  105. c++;
  106. }
  107. // Plan B: reset backup domain
  108. if(!RTC_CLOCK_IS_READY()) {
  109. target_led_control("-R.R.R.");
  110. LL_RCC_ForceBackupDomainReset();
  111. LL_RCC_ReleaseBackupDomainReset();
  112. NVIC_SystemReset();
  113. }
  114. // Set RTC domain clock to LSE
  115. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  116. // Enable LSE CSS
  117. LL_RCC_LSE_EnableCSS();
  118. }
  119. // Enable clocking
  120. LL_RCC_EnableRTC();
  121. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  122. }
  123. void target_version_save(void) {
  124. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR1, (uint32_t)version_get());
  125. }
  126. void target_usb_wire_reset() {
  127. LL_GPIO_ResetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  128. LL_mDelay(10);
  129. LL_GPIO_SetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  130. }
  131. void target_display_init() {
  132. // Prepare gpio
  133. hal_gpio_init_simple(&gpio_display_rst, GpioModeOutputPushPull);
  134. hal_gpio_init_simple(&gpio_display_di, GpioModeOutputPushPull);
  135. // Initialize
  136. u8g2_t fb;
  137. u8g2_Setup_st7565_erc12864_alt_f(&fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
  138. u8g2_InitDisplay(&fb);
  139. u8g2_SetContrast(&fb, 36);
  140. // Create payload
  141. u8g2_ClearBuffer(&fb);
  142. u8g2_SetDrawColor(&fb, 0x01);
  143. u8g2_SetFont(&fb, u8g2_font_helvB08_tf);
  144. u8g2_DrawStr(&fb, 2, 8, "Recovery & Update Mode");
  145. u8g2_DrawXBM(&fb, 49, 14, 30, 23, I_Warning_30x23_0);
  146. u8g2_DrawStr(&fb, 2, 50, "DFU Bootloader activated");
  147. u8g2_DrawStr(&fb, 6, 62, "www.flipp.dev/recovery");
  148. // Send buffer
  149. u8g2_SetPowerSave(&fb, 0);
  150. u8g2_SendBuffer(&fb);
  151. }
  152. void target_init() {
  153. target_clock_init();
  154. target_gpio_init();
  155. furi_hal_init();
  156. target_led_control("RGB");
  157. target_rtc_init();
  158. target_version_save();
  159. target_usb_wire_reset();
  160. // Errata 2.2.9, Flash OPTVERR flag is always set after system reset
  161. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  162. }
  163. int target_is_dfu_requested() {
  164. if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) {
  165. // Default system state is tainted
  166. // We must ensure that MCU is cleanly booted
  167. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
  168. NVIC_SystemReset();
  169. } else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
  170. return 1;
  171. }
  172. LL_mDelay(100);
  173. if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. void target_switch(void* offset) {
  179. asm volatile("ldr r3, [%0] \n"
  180. "msr msp, r3 \n"
  181. "ldr r3, [%1] \n"
  182. "mov pc, r3 \n"
  183. :
  184. : "r"(offset), "r"(offset + 0x4)
  185. : "r3");
  186. }
  187. void target_switch2dfu() {
  188. target_led_control("B");
  189. furi_hal_light_set(LightBacklight, 0xFF);
  190. target_display_init();
  191. // Mark system as tainted, it will be soon
  192. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
  193. // Remap memory to system bootloader
  194. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SYSTEMFLASH);
  195. // Jump
  196. target_switch(0x0);
  197. }
  198. void target_switch2os() {
  199. target_led_control("G");
  200. SCB->VTOR = OS_OFFSET;
  201. target_switch((void*)(BOOT_ADDRESS + OS_OFFSET));
  202. }