target.c 6.4 KB

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