target.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Boot request enum
  12. #define BOOT_REQUEST_NONE 0x00000000
  13. #define BOOT_REQUEST_DFU 0xDF00B000
  14. // Boot to DFU pin
  15. #define BOOT_DFU_PORT GPIOB
  16. #define BOOT_DFU_PIN LL_GPIO_PIN_11
  17. // USB pins
  18. #define BOOT_USB_PORT GPIOA
  19. #define BOOT_USB_DM_PIN LL_GPIO_PIN_11
  20. #define BOOT_USB_DP_PIN LL_GPIO_PIN_12
  21. #define BOOT_USB_PIN (BOOT_USB_DM_PIN | BOOT_USB_DP_PIN)
  22. void target_led_set_red(uint8_t value) {
  23. }
  24. void target_led_set_green(uint8_t value) {
  25. }
  26. void target_led_set_blue(uint8_t value) {
  27. }
  28. void target_led_set_backlight(uint8_t value) {
  29. }
  30. void target_led_control(char* c) {
  31. target_led_set_red(0x00);
  32. target_led_set_green(0x00);
  33. target_led_set_blue(0x00);
  34. do {
  35. if(*c == 'R') {
  36. target_led_set_red(0xFF);
  37. } else if(*c == 'G') {
  38. target_led_set_green(0xFF);
  39. } else if(*c == 'B') {
  40. target_led_set_blue(0xFF);
  41. } else if(*c == '.') {
  42. LL_mDelay(125);
  43. target_led_set_red(0x00);
  44. target_led_set_green(0x00);
  45. target_led_set_blue(0x00);
  46. LL_mDelay(125);
  47. } else if(*c == '-') {
  48. LL_mDelay(250);
  49. target_led_set_red(0x00);
  50. target_led_set_green(0x00);
  51. target_led_set_blue(0x00);
  52. LL_mDelay(250);
  53. } else if(*c == '|') {
  54. target_led_set_red(0x00);
  55. target_led_set_green(0x00);
  56. target_led_set_blue(0x00);
  57. }
  58. c++;
  59. } while(*c != 0);
  60. }
  61. void clock_init() {
  62. LL_Init1msTick(4000000);
  63. LL_SetSystemCoreClock(4000000);
  64. }
  65. void gpio_init() {
  66. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  67. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  68. // USB D+
  69. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_MODE_OUTPUT);
  70. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  71. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  72. // USB D-
  73. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_MODE_OUTPUT);
  74. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  75. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  76. // Button: back
  77. LL_GPIO_SetPinMode(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_MODE_INPUT);
  78. LL_GPIO_SetPinPull(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_PULL_UP);
  79. }
  80. void rtc_init() {
  81. // LSE and RTC
  82. LL_PWR_EnableBkUpAccess();
  83. if(!LL_RCC_LSE_IsReady()) {
  84. // Try to start LSE normal way
  85. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_MEDIUMLOW);
  86. LL_RCC_LSE_Enable();
  87. uint32_t c = 0;
  88. while(!LL_RCC_LSE_IsReady() && c < 200) {
  89. LL_mDelay(10);
  90. c++;
  91. }
  92. // Plan B: reset backup domain
  93. if(!LL_RCC_LSE_IsReady()) {
  94. target_led_control("-R.R.R.");
  95. LL_RCC_ForceBackupDomainReset();
  96. LL_RCC_ReleaseBackupDomainReset();
  97. NVIC_SystemReset();
  98. }
  99. // Set RTC domain clock to LSE
  100. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  101. }
  102. // Enable clocking
  103. LL_RCC_EnableRTC();
  104. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  105. }
  106. void usb_wire_reset() {
  107. LL_GPIO_ResetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  108. LL_mDelay(10);
  109. LL_GPIO_SetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  110. }
  111. void target_init() {
  112. clock_init();
  113. gpio_init();
  114. rtc_init();
  115. usb_wire_reset();
  116. // Errata 2.2.9, Flash OPTVERR flag is always set after system reset
  117. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  118. }
  119. int target_is_dfu_requested() {
  120. if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
  121. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_NONE);
  122. return 1;
  123. }
  124. LL_mDelay(100);
  125. if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. void target_switch(void* offset) {
  131. asm volatile("ldr r3, [%0] \n"
  132. "msr msp, r3 \n"
  133. "ldr r3, [%1] \n"
  134. "mov pc, r3 \n"
  135. :
  136. : "r"(offset), "r"(offset + 0x4)
  137. : "r3");
  138. }
  139. void target_switch2dfu() {
  140. target_led_control("B");
  141. // Remap memory to system bootloader
  142. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SYSTEMFLASH);
  143. target_switch(0x0);
  144. }
  145. void target_switch2os() {
  146. target_led_control("G");
  147. SCB->VTOR = BOOT_ADDRESS + OS_OFFSET;
  148. target_switch((void*)(BOOT_ADDRESS + OS_OFFSET));
  149. }