target.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_DFU_128x50[] = {
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x07, 0x00, 0x00, 0x00,
  16. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x38, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0xC0, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8F, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x7F, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
  23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x75, 0x00, 0x00, 0xF0, 0x3F, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x0A, 0x00, 0x00, 0x0F, 0x60, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xE0, 0x0F, 0x00, 0xC0, 0xE0, 0x4F, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x30, 0x1E, 0x90, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x18, 0x00, 0x8C, 0x01, 0xA0, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x81, 0xFF, 0x19, 0x00, 0x63, 0x00, 0xC0, 0xF0, 0x07,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x60, 0x5E, 0x1F, 0x80, 0x18, 0x00, 0xE0, 0x0E, 0x18,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x18, 0xAF, 0x0F, 0x40, 0x06, 0x00, 0xF8, 0x01, 0x20,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x06, 0x57, 0x01, 0x20, 0x01, 0x00, 0x78, 0x00, 0x3E,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x84, 0x81, 0xAF, 0x02, 0x90, 0x00, 0x00, 0x38, 0x80, 0x41,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x80, 0x57, 0x01, 0x48, 0x00, 0x00, 0x10, 0x60, 0x40,
  34. 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x10, 0x80, 0xAB, 0x00, 0x24, 0x00, 0x00, 0x08, 0x10, 0x40,
  35. 0x3F, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0C, 0xC0, 0x57, 0x01, 0x12, 0x00, 0x00, 0x04, 0x08, 0x40,
  36. 0xC0, 0x0F, 0x00, 0x00, 0xC0, 0x07, 0x03, 0xF0, 0xAB, 0x00, 0x0A, 0x00, 0x00, 0x02, 0x04, 0x40,
  37. 0x00, 0xF0, 0x1F, 0x80, 0x3F, 0xC0, 0x00, 0xFC, 0x55, 0x01, 0x05, 0xE0, 0x00, 0x01, 0x04, 0x40,
  38. 0x00, 0x00, 0xE0, 0x7F, 0x00, 0x30, 0x00, 0xFF, 0xAB, 0x00, 0x05, 0xE0, 0x80, 0x00, 0x02, 0x40,
  39. 0x0F, 0x00, 0x00, 0x00, 0x80, 0x0F, 0xE0, 0xCF, 0x55, 0x81, 0x02, 0xF0, 0x40, 0x00, 0x02, 0x40,
  40. 0xF0, 0x0F, 0x00, 0x00, 0x7F, 0x00, 0xFE, 0xC3, 0xAB, 0x80, 0x02, 0x78, 0x20, 0x00, 0x01, 0x40,
  41. 0x00, 0xF0, 0xFF, 0xFF, 0x00, 0xF0, 0xFF, 0xC0, 0xD5, 0x81, 0x01, 0x7E, 0x10, 0x80, 0x00, 0x20,
  42. 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x0F, 0xE0, 0xFA, 0x83, 0xC1, 0x3F, 0x08, 0x80, 0x00, 0x20,
  43. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x01, 0xD8, 0x07, 0x83, 0xF1, 0x1F, 0x04, 0x40, 0x00, 0x20,
  44. 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0x0F, 0x80, 0xC7, 0x01, 0x83, 0xF1, 0x0F, 0x00, 0x20, 0x00, 0x10,
  45. 0xE0, 0xFF, 0xFF, 0xFF, 0x3F, 0xC0, 0x7F, 0x40, 0x80, 0x83, 0xE1, 0x01, 0x00, 0x20, 0x00, 0x18,
  46. 0xFC, 0xFF, 0xFF, 0xFF, 0x03, 0x3F, 0x00, 0x20, 0xFC, 0x83, 0x01, 0x00, 0x00, 0x10, 0x00, 0x18,
  47. 0xFF, 0xFF, 0xFF, 0x3F, 0xF0, 0x00, 0x00, 0x10, 0xD7, 0x01, 0x03, 0x00, 0x00, 0x08, 0x00, 0x1C,
  48. 0xFF, 0xFF, 0x01, 0x00, 0x0F, 0x00, 0x00, 0x88, 0xAB, 0x02, 0xE3, 0x01, 0x00, 0x08, 0x00, 0x0C,
  49. 0xFF, 0x07, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xC4, 0x55, 0x05, 0x1E, 0x00, 0x00, 0x04, 0x00, 0x0E,
  50. 0x7F, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0xA3, 0xAB, 0x02, 0x06, 0x00, 0x00, 0x02, 0x00, 0x0F,
  51. 0x0F, 0x00, 0x80, 0x03, 0x00, 0x00, 0xC0, 0x10, 0x57, 0x05, 0x02, 0x00, 0x00, 0x01, 0x80, 0x07,
  52. 0x03, 0x00, 0x70, 0x00, 0x00, 0x00, 0x30, 0x08, 0xAB, 0x0A, 0x02, 0x00, 0xC0, 0x00, 0xC0, 0x07,
  53. 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0C, 0x84, 0x57, 0x15, 0x01, 0x00, 0x30, 0x00, 0xE0, 0x07,
  54. 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0xC3, 0xFF, 0x2A, 0x01, 0x00, 0x0C, 0x00, 0xF0, 0x0F,
  55. 0x00, 0xC0, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0xE0, 0xFE, 0x55, 0x01, 0x82, 0x03, 0x00, 0xF8, 0x15,
  56. 0x00, 0x30, 0x00, 0x00, 0x00, 0x1C, 0x30, 0x78, 0xFE, 0xAA, 0x01, 0x7C, 0x00, 0x00, 0xFC, 0x23,
  57. 0x00, 0x0E, 0x00, 0x00, 0xC0, 0x03, 0x0C, 0x3C, 0x7F, 0x5D, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x45,
  58. 0xC0, 0x01, 0x00, 0x00, 0x3E, 0x00, 0x02, 0x8F, 0xBF, 0xAE, 0x03, 0x00, 0x00, 0xC0, 0xFF, 0x82,
  59. 0x30, 0x00, 0x00, 0xC0, 0x01, 0x80, 0xC1, 0x43, 0xFE, 0x5D, 0x01, 0x00, 0x00, 0xF0, 0xFF, 0x05,
  60. 0x0F, 0x00, 0x80, 0x3F, 0x00, 0x60, 0xF0, 0x31, 0xF6, 0xAE, 0x03, 0x00, 0x00, 0xFA, 0xAF, 0x02,
  61. 0xFC, 0xFF, 0x7F, 0x00, 0x00, 0x18, 0x7C, 0x08, 0x23, 0xFF, 0x05, 0x00, 0x00, 0xFD, 0x55, 0x01,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x1F, 0x84, 0x30, 0xFE, 0x0A, 0x00, 0x00, 0xAA, 0xAA, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x80, 0xF1, 0x07, 0x43, 0x18, 0xFF, 0x15, 0x00, 0x00, 0x54, 0x15, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, 0x80, 0x20, 0x8C, 0xFF, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. };
  66. // Boot request enum
  67. #define BOOT_REQUEST_TAINTED 0x00000000
  68. #define BOOT_REQUEST_CLEAN 0xDADEDADE
  69. #define BOOT_REQUEST_DFU 0xDF00B000
  70. // Boot to DFU pin
  71. #define BOOT_DFU_PORT GPIOB
  72. #define BOOT_DFU_PIN LL_GPIO_PIN_11
  73. // USB pins
  74. #define BOOT_USB_PORT GPIOA
  75. #define BOOT_USB_DM_PIN LL_GPIO_PIN_11
  76. #define BOOT_USB_DP_PIN LL_GPIO_PIN_12
  77. #define BOOT_USB_PIN (BOOT_USB_DM_PIN | BOOT_USB_DP_PIN)
  78. #define RTC_CLOCK_IS_READY() (LL_RCC_LSE_IsReady() && LL_RCC_LSI1_IsReady())
  79. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
  80. uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
  81. void target_led_control(char* c) {
  82. furi_hal_light_set(LightRed, 0x00);
  83. furi_hal_light_set(LightGreen, 0x00);
  84. furi_hal_light_set(LightBlue, 0x00);
  85. do {
  86. if(*c == 'R') {
  87. furi_hal_light_set(LightRed, 0xFF);
  88. } else if(*c == 'G') {
  89. furi_hal_light_set(LightGreen, 0xFF);
  90. } else if(*c == 'B') {
  91. furi_hal_light_set(LightBlue, 0xFF);
  92. } else if(*c == '.') {
  93. LL_mDelay(125);
  94. furi_hal_light_set(LightRed, 0x00);
  95. furi_hal_light_set(LightGreen, 0x00);
  96. furi_hal_light_set(LightBlue, 0x00);
  97. LL_mDelay(125);
  98. } else if(*c == '-') {
  99. LL_mDelay(250);
  100. furi_hal_light_set(LightRed, 0x00);
  101. furi_hal_light_set(LightGreen, 0x00);
  102. furi_hal_light_set(LightBlue, 0x00);
  103. LL_mDelay(250);
  104. } else if(*c == '|') {
  105. furi_hal_light_set(LightRed, 0x00);
  106. furi_hal_light_set(LightGreen, 0x00);
  107. furi_hal_light_set(LightBlue, 0x00);
  108. }
  109. c++;
  110. } while(*c != 0);
  111. }
  112. void target_clock_init() {
  113. LL_Init1msTick(4000000);
  114. LL_SetSystemCoreClock(4000000);
  115. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  116. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  117. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
  118. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOD);
  119. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOE);
  120. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOH);
  121. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  122. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
  123. }
  124. void target_gpio_init() {
  125. // USB D+
  126. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_MODE_OUTPUT);
  127. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  128. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  129. // USB D-
  130. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_MODE_OUTPUT);
  131. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  132. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  133. // Button: back
  134. LL_GPIO_SetPinMode(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_MODE_INPUT);
  135. LL_GPIO_SetPinPull(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_PULL_UP);
  136. }
  137. void target_rtc_init() {
  138. // LSE and RTC
  139. LL_PWR_EnableBkUpAccess();
  140. if(!RTC_CLOCK_IS_READY()) {
  141. // Start LSI1 needed for CSS
  142. LL_RCC_LSI1_Enable();
  143. // Try to start LSE normal way
  144. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_HIGH);
  145. LL_RCC_LSE_Enable();
  146. uint32_t c = 0;
  147. while(!RTC_CLOCK_IS_READY() && c < 200) {
  148. LL_mDelay(10);
  149. c++;
  150. }
  151. // Plan B: reset backup domain
  152. if(!RTC_CLOCK_IS_READY()) {
  153. target_led_control("-R.R.R.");
  154. LL_RCC_ForceBackupDomainReset();
  155. LL_RCC_ReleaseBackupDomainReset();
  156. NVIC_SystemReset();
  157. }
  158. // Set RTC domain clock to LSE
  159. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  160. // Enable LSE CSS
  161. LL_RCC_LSE_EnableCSS();
  162. }
  163. // Enable clocking
  164. LL_RCC_EnableRTC();
  165. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  166. }
  167. void target_version_save(void) {
  168. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR1, (uint32_t)version_get());
  169. }
  170. void target_usb_wire_reset() {
  171. LL_GPIO_ResetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  172. }
  173. void target_display_init() {
  174. // Prepare gpio
  175. hal_gpio_init_simple(&gpio_display_rst, GpioModeOutputPushPull);
  176. hal_gpio_init_simple(&gpio_display_di, GpioModeOutputPushPull);
  177. // Initialize
  178. u8g2_t fb;
  179. u8g2_Setup_st7565_erc12864_alt_f(&fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
  180. u8g2_InitDisplay(&fb);
  181. u8g2_SetContrast(&fb, 36);
  182. // Create payload
  183. u8g2_ClearBuffer(&fb);
  184. u8g2_SetDrawColor(&fb, 0x01);
  185. u8g2_SetFont(&fb, u8g2_font_helvB08_tf);
  186. u8g2_DrawXBM(&fb, 0, 64 - 50, 128, 50, I_DFU_128x50);
  187. u8g2_DrawStr(&fb, 2, 8, "Update & Recovery Mode");
  188. u8g2_DrawStr(&fb, 2, 21, "DFU started");
  189. // Send buffer
  190. u8g2_SetPowerSave(&fb, 0);
  191. u8g2_SendBuffer(&fb);
  192. }
  193. void target_init() {
  194. target_clock_init();
  195. target_gpio_init();
  196. furi_hal_init();
  197. target_led_control("RGB");
  198. target_rtc_init();
  199. target_version_save();
  200. target_usb_wire_reset();
  201. // Errata 2.2.9, Flash OPTVERR flag is always set after system reset
  202. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  203. }
  204. int target_is_dfu_requested() {
  205. if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) {
  206. // Default system state is tainted
  207. // We must ensure that MCU is cleanly booted
  208. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
  209. NVIC_SystemReset();
  210. } else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
  211. return 1;
  212. }
  213. LL_mDelay(100);
  214. if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {
  215. return 1;
  216. }
  217. return 0;
  218. }
  219. void target_switch(void* offset) {
  220. asm volatile("ldr r3, [%0] \n"
  221. "msr msp, r3 \n"
  222. "ldr r3, [%1] \n"
  223. "mov pc, r3 \n"
  224. :
  225. : "r"(offset), "r"(offset + 0x4)
  226. : "r3");
  227. }
  228. void target_switch2dfu() {
  229. target_led_control("B");
  230. furi_hal_light_set(LightBacklight, 0xFF);
  231. target_display_init();
  232. // Mark system as tainted, it will be soon
  233. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
  234. // Remap memory to system bootloader
  235. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SYSTEMFLASH);
  236. // Jump
  237. target_switch(0x0);
  238. }
  239. void target_switch2os() {
  240. target_led_control("G");
  241. SCB->VTOR = OS_OFFSET;
  242. target_switch((void*)(BOOT_ADDRESS + OS_OFFSET));
  243. }