target.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // LCD backlight
  18. #define BOOT_LCD_BL_PORT GPIOA
  19. #define BOOT_LCD_BL_PIN LL_GPIO_PIN_15
  20. // LEDs
  21. #define LED_RED_PORT GPIOA
  22. #define LED_RED_PIN LL_GPIO_PIN_1
  23. #define LED_GREEN_PORT GPIOA
  24. #define LED_GREEN_PIN LL_GPIO_PIN_2
  25. #define LED_BLUE_PORT GPIOA
  26. #define LED_BLUE_PIN LL_GPIO_PIN_3
  27. // USB pins
  28. #define BOOT_USB_PORT GPIOA
  29. #define BOOT_USB_DM_PIN LL_GPIO_PIN_11
  30. #define BOOT_USB_DP_PIN LL_GPIO_PIN_12
  31. #define BOOT_USB_PIN (BOOT_USB_DM_PIN | BOOT_USB_DP_PIN)
  32. #define RTC_CLOCK_IS_READY() (LL_RCC_LSE_IsReady() && LL_RCC_LSI1_IsReady())
  33. void target_led_control(char* c) {
  34. LL_GPIO_SetOutputPin(LED_RED_PORT, LED_RED_PIN);
  35. LL_GPIO_SetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
  36. LL_GPIO_SetOutputPin(LED_BLUE_PORT, LED_BLUE_PIN);
  37. do {
  38. if(*c == 'R') {
  39. LL_GPIO_ResetOutputPin(LED_RED_PORT, LED_RED_PIN);
  40. } else if(*c == 'G') {
  41. LL_GPIO_ResetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
  42. } else if(*c == 'B') {
  43. LL_GPIO_ResetOutputPin(LED_BLUE_PORT, LED_BLUE_PIN);
  44. } else if(*c == '.') {
  45. LL_mDelay(125);
  46. LL_GPIO_SetOutputPin(LED_RED_PORT, LED_RED_PIN);
  47. LL_GPIO_SetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
  48. LL_GPIO_SetOutputPin(LED_BLUE_PORT, LED_BLUE_PIN);
  49. LL_mDelay(125);
  50. } else if(*c == '-') {
  51. LL_mDelay(250);
  52. LL_GPIO_SetOutputPin(LED_RED_PORT, LED_RED_PIN);
  53. LL_GPIO_SetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
  54. LL_GPIO_SetOutputPin(LED_BLUE_PORT, LED_BLUE_PIN);
  55. LL_mDelay(250);
  56. } else if(*c == '|') {
  57. LL_GPIO_SetOutputPin(LED_RED_PORT, LED_RED_PIN);
  58. LL_GPIO_SetOutputPin(LED_GREEN_PORT, LED_GREEN_PIN);
  59. LL_GPIO_SetOutputPin(LED_BLUE_PORT, LED_BLUE_PIN);
  60. }
  61. c++;
  62. } while(*c != 0);
  63. }
  64. void clock_init() {
  65. LL_Init1msTick(4000000);
  66. LL_SetSystemCoreClock(4000000);
  67. }
  68. void gpio_init() {
  69. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  70. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  71. // USB D+
  72. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_MODE_OUTPUT);
  73. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  74. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DP_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  75. // USB D-
  76. LL_GPIO_SetPinMode(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_MODE_OUTPUT);
  77. LL_GPIO_SetPinSpeed(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  78. LL_GPIO_SetPinOutputType(BOOT_USB_PORT, BOOT_USB_DM_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  79. // Button: back
  80. LL_GPIO_SetPinMode(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_MODE_INPUT);
  81. LL_GPIO_SetPinPull(BOOT_DFU_PORT, BOOT_DFU_PIN, LL_GPIO_PULL_UP);
  82. // Display backlight
  83. LL_GPIO_SetPinMode(BOOT_LCD_BL_PORT, BOOT_LCD_BL_PIN, LL_GPIO_MODE_OUTPUT);
  84. LL_GPIO_SetPinSpeed(BOOT_LCD_BL_PORT, BOOT_LCD_BL_PIN, LL_GPIO_SPEED_FREQ_LOW);
  85. LL_GPIO_SetPinOutputType(BOOT_LCD_BL_PORT, BOOT_LCD_BL_PIN, LL_GPIO_OUTPUT_PUSHPULL);
  86. // LEDs
  87. LL_GPIO_SetPinMode(LED_RED_PORT, LED_RED_PIN, LL_GPIO_MODE_OUTPUT);
  88. LL_GPIO_SetPinOutputType(LED_RED_PORT, LED_RED_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  89. LL_GPIO_SetPinMode(LED_GREEN_PORT, LED_GREEN_PIN, LL_GPIO_MODE_OUTPUT);
  90. LL_GPIO_SetPinOutputType(LED_GREEN_PORT, LED_GREEN_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  91. LL_GPIO_SetPinMode(LED_BLUE_PORT, LED_BLUE_PIN, LL_GPIO_MODE_OUTPUT);
  92. LL_GPIO_SetPinOutputType(LED_BLUE_PORT, LED_BLUE_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  93. }
  94. void rtc_init() {
  95. // LSE and RTC
  96. LL_PWR_EnableBkUpAccess();
  97. if(!RTC_CLOCK_IS_READY()) {
  98. // Start LSI1 needed for CSS
  99. LL_RCC_LSI1_Enable();
  100. // Try to start LSE normal way
  101. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_MEDIUMLOW);
  102. LL_RCC_LSE_Enable();
  103. uint32_t c = 0;
  104. while(!RTC_CLOCK_IS_READY() && c < 200) {
  105. LL_mDelay(10);
  106. c++;
  107. }
  108. // Plan B: reset backup domain
  109. if(!RTC_CLOCK_IS_READY()) {
  110. target_led_control("-R.R.R.");
  111. LL_RCC_ForceBackupDomainReset();
  112. LL_RCC_ReleaseBackupDomainReset();
  113. NVIC_SystemReset();
  114. }
  115. // Set RTC domain clock to LSE
  116. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  117. // Enable LSE CSS
  118. LL_RCC_LSE_EnableCSS();
  119. }
  120. // Enable clocking
  121. LL_RCC_EnableRTC();
  122. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  123. }
  124. void lcd_backlight_on() {
  125. LL_GPIO_SetOutputPin(BOOT_LCD_BL_PORT, BOOT_LCD_BL_PIN);
  126. }
  127. void usb_wire_reset() {
  128. LL_GPIO_ResetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  129. LL_mDelay(10);
  130. LL_GPIO_SetOutputPin(BOOT_USB_PORT, BOOT_USB_PIN);
  131. }
  132. void target_init() {
  133. clock_init();
  134. gpio_init();
  135. rtc_init();
  136. usb_wire_reset();
  137. // Errata 2.2.9, Flash OPTVERR flag is always set after system reset
  138. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  139. }
  140. int target_is_dfu_requested() {
  141. if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
  142. LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_NONE);
  143. return 1;
  144. }
  145. LL_mDelay(100);
  146. if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. void target_switch(void* offset) {
  152. asm volatile("ldr r3, [%0] \n"
  153. "msr msp, r3 \n"
  154. "ldr r3, [%1] \n"
  155. "mov pc, r3 \n"
  156. :
  157. : "r"(offset), "r"(offset + 0x4)
  158. : "r3");
  159. }
  160. void target_switch2dfu() {
  161. target_led_control("B");
  162. // Remap memory to system bootloader
  163. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SYSTEMFLASH);
  164. target_switch(0x0);
  165. }
  166. void target_switch2os() {
  167. target_led_control("G");
  168. SCB->VTOR = BOOT_ADDRESS + OS_OFFSET;
  169. target_switch((void*)(BOOT_ADDRESS + OS_OFFSET));
  170. }