target.c 4.8 KB

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