Advertisement
ALTracer

BMP blackpill-f4 DFU

Jun 6th, 2023
1,475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 8.05 KB | None | 0 0
  1. From ea411bd77fcec6c19b2cd80c554d68bc24451b64 Mon Sep 17 00:00:00 2001
  2. From: Denis Tolstov <dtolstov@vniir.ru>
  3. Date: Tue, 6 Jun 2023 20:02:40 +0300
  4. Subject: [PATCH 1/3] platforms/blackpill-f4: Allow external debugging
  5.  
  6. * Dispatch newlib/libgloss as nosys or rdimon
  7. * Enable nano.specs to reduce firmware size
  8. * Declare debugging available as on native
  9. ---
  10. src/platforms/common/blackpill-f4/Makefile.inc   | 9 ++++++++-
  11.  src/platforms/common/blackpill-f4/blackpill-f4.h | 5 +++++
  12.  2 files changed, 13 insertions(+), 1 deletion(-)
  13.  
  14. diff --git a/src/platforms/common/blackpill-f4/Makefile.inc b/src/platforms/common/blackpill-f4/Makefile.inc
  15. index 62a6cf6..e552036 100644
  16. --- a/src/platforms/common/blackpill-f4/Makefile.inc
  17. +++ b/src/platforms/common/blackpill-f4/Makefile.inc
  18. @@ -15,11 +15,11 @@ CFLAGS +=                           \
  19.     -Iplatforms/common/blackpill-f4
  20.  
  21.  LDFLAGS_BOOT =              \
  22. +   --specs=nano.specs      \
  23.     -lopencm3_stm32f4       \
  24.     -Wl,-T,$(LINKER_SCRIPT) \
  25.     -nostartfiles           \
  26.     -lc                     \
  27. -   -lnosys                 \
  28.     -Wl,-Map=mapfile        \
  29.     -mthumb                 \
  30.     -mcpu=cortex-m4         \
  31. @@ -37,6 +37,13 @@ LDFLAGS += $(LDFLAGS_BOOT)
  32.  CFLAGS += -DDFU_SERIAL_LENGTH=13
  33.  endif
  34.  
  35. +ifeq ($(ENABLE_DEBUG), 1)
  36. +# Use rdimon when there is another JTAG adapter on our BMP adapter
  37. +LDFLAGS += --specs=rdimon.specs
  38. +else
  39. +LDFLAGS += --specs=nosys.specs
  40. +endif
  41. +
  42.  ifdef ALTERNATIVE_PINOUT
  43.  CFLAGS += -DALTERNATIVE_PINOUT=$(ALTERNATIVE_PINOUT)
  44.  endif
  45. diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.h b/src/platforms/common/blackpill-f4/blackpill-f4.h
  46. index 0342d3f..ec98775 100644
  47. --- a/src/platforms/common/blackpill-f4/blackpill-f4.h
  48. +++ b/src/platforms/common/blackpill-f4/blackpill-f4.h
  49. @@ -27,6 +27,11 @@
  50.  #include "timing.h"
  51.  #include "timing_stm32.h"
  52.  
  53. +#ifdef ENABLE_DEBUG
  54. +#define PLATFORM_HAS_DEBUG
  55. +extern bool debug_bmp;
  56. +#endif
  57. +
  58.  #define PLATFORM_HAS_TRACESWO
  59.  
  60.  /* Error handling for ALTERNATIVE_PINOUT
  61. --
  62. 2.34.1
  63.  
  64. From 9095f29cf6759d7dea5e2d0c1e26e126b62b1f9b Mon Sep 17 00:00:00 2001
  65. From: Denis Tolstov <dtolstov@vniir.ru>
  66. Date: Tue, 6 Jun 2023 20:11:35 +0300
  67. Subject: [PATCH 2/3] platforms/blackpill-f4: Enable BMP DFU build variant
  68.  
  69. * BMP DFU uploads are ~2.4x faster than STM32F411 BootROM DFU
  70. * Use 96MHz clocks in DFU (and in APP), revisit for F401 & 84MHz
  71. * Use the PA0 pushbutton active-low and pulled-up to control DFU entry
  72. ---
  73. src/platforms/common/blackpill-f4/Makefile.inc   |  1 +
  74.  src/platforms/common/blackpill-f4/blackpill-f4.c | 13 +++++++++++--
  75.  src/platforms/common/blackpill-f4/usbdfu.c       | 11 ++++++++---
  76.  3 files changed, 20 insertions(+), 5 deletions(-)
  77.  
  78. diff --git a/src/platforms/common/blackpill-f4/Makefile.inc b/src/platforms/common/blackpill-f4/Makefile.inc
  79. index e552036..a231b20 100644
  80. --- a/src/platforms/common/blackpill-f4/Makefile.inc
  81. +++ b/src/platforms/common/blackpill-f4/Makefile.inc
  82. @@ -31,6 +31,7 @@ LDFLAGS_BOOT =              \
  83.  ifeq ($(BMP_BOOTLOADER), 1)
  84.  $(info  Load address 0x08004000 for BMPBootloader)
  85.  LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8004000
  86. +CFLAGS += -DAPP_START=0x08004000
  87.  CFLAGS += -DDFU_SERIAL_LENGTH=9
  88.  else
  89.  LDFLAGS += $(LDFLAGS_BOOT)
  90. diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c
  91. index 0824dd9..dd53be1 100644
  92. --- a/src/platforms/common/blackpill-f4/blackpill-f4.c
  93. +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c
  94. @@ -42,16 +42,23 @@ extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-
  95.  
  96.  void platform_init(void)
  97.  {
  98. +#ifdef USE_STM32F4_MASKROM
  99.     volatile uint32_t *magic = (uint32_t *)&_ebss;
  100. +#endif
  101.     /* Enable GPIO peripherals */
  102.     rcc_periph_clock_enable(RCC_GPIOA);
  103.     rcc_periph_clock_enable(RCC_GPIOC);
  104.     rcc_periph_clock_enable(RCC_GPIOB);
  105.  
  106. +#ifdef USE_STM32F4_MASKROM
  107. +
  108. +   /* Blackpill board has a floating button on PA0. Pull it up and use as active-low. */
  109. +   gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO0);
  110. +
  111.  #pragma GCC diagnostic push
  112.  #pragma GCC diagnostic ignored "-Warray-bounds"
  113.     /* Check the USER button */
  114. -   if (gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) {
  115. +   if (!gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) {
  116.         magic[0] = 0;
  117.         magic[1] = 0;
  118.         /* Assert blue LED as indicator we are in the bootloader */
  119. @@ -67,7 +74,9 @@ void platform_init(void)
  120.         scb_reset_core();
  121.     }
  122.  #pragma GCC diagnostic pop
  123. -   rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
  124. +
  125. +#endif
  126. +   rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_96MHZ]);
  127.  
  128.     /* Enable peripherals */
  129.     rcc_periph_clock_enable(RCC_OTGFS);
  130. diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c
  131. index 24a96fe..1883b2d 100644
  132. --- a/src/platforms/common/blackpill-f4/usbdfu.c
  133. +++ b/src/platforms/common/blackpill-f4/usbdfu.c
  134. @@ -22,6 +22,7 @@
  135.  #include <libopencm3/stm32/rcc.h>
  136.  #include <libopencm3/stm32/gpio.h>
  137.  #include <libopencm3/cm3/scb.h>
  138. +#include <libopencm3/usb/dwc/otg_fs.h>
  139.  
  140.  #include "usbdfu.h"
  141.  #include "general.h"
  142. @@ -43,19 +44,22 @@ int main(void)
  143.     volatile uint32_t *magic = (uint32_t *)&_ebss;
  144.     rcc_periph_clock_enable(RCC_GPIOA);
  145.  
  146. +   /* Blackpill board has a floating button on PA0. Pull it up and use as active-low. */
  147. +   gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO0);
  148. +
  149.  #pragma GCC diagnostic push
  150.  #pragma GCC diagnostic ignored "-Warray-bounds"
  151. -   if (gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) {
  152. +   if (!gpio_get(GPIOA, GPIO0) || (magic[0] == BOOTMAGIC0 && magic[1] == BOOTMAGIC1)) {
  153.         magic[0] = 0;
  154.         magic[1] = 0;
  155.     } else
  156.         dfu_jump_app_if_valid();
  157.  #pragma GCC diagnostic pop
  158.  
  159. -   rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
  160. +   rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_96MHZ]);
  161.  
  162.     /* Assert blue LED as indicator we are in the bootloader */
  163. -   rcc_periph_clock_enable(RCC_GPIOD);
  164. +   rcc_periph_clock_enable(RCC_GPIOC);
  165.     gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_BOOTLOADER);
  166.     gpio_set(LED_PORT, LED_BOOTLOADER);
  167.  
  168. @@ -68,6 +72,7 @@ int main(void)
  169.  
  170.     dfu_protect(false);
  171.     dfu_init(&USB_DRIVER);
  172. +
  173.     dfu_main();
  174.  }
  175.  
  176. --
  177. 2.34.1
  178.  
  179. From ce791cd20e6f183162f26083e0ea7fc6a57a1550 Mon Sep 17 00:00:00 2001
  180. From: Denis Tolstov <dtolstov@vniir.ru>
  181. Date: Tue, 6 Jun 2023 20:18:16 +0300
  182. Subject: [PATCH 3/3] platforms/blackpill-f4: Fix DWC USB init
  183.  
  184. ---
  185. src/platforms/common/blackpill-f4/blackpill-f4.c | 6 ++++++
  186.  src/platforms/common/blackpill-f4/usbdfu.c       | 6 ++++++
  187.  2 files changed, 12 insertions(+)
  188.  
  189. diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c
  190. index dd53be1..83fa603 100644
  191. --- a/src/platforms/common/blackpill-f4/blackpill-f4.c
  192. +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c
  193. @@ -82,9 +82,15 @@ void platform_init(void)
  194.     rcc_periph_clock_enable(RCC_OTGFS);
  195.     rcc_periph_clock_enable(RCC_CRC);
  196.  
  197. +#if 0
  198.     /* Set up USB Pins and alternate function */
  199.     gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO11 | GPIO12);
  200.     gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO10 | GPIO11 | GPIO12);
  201. +#else
  202. +   /* PA9/PA10 are not routed to USB on Blackpill-F4 */
  203. +   gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
  204. +   gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12);
  205. +#endif
  206.  
  207.     GPIOA_OSPEEDR &= 0x3c00000cU;
  208.     GPIOA_OSPEEDR |= 0x28000008U;
  209. diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c
  210. index 1883b2d..107c71a 100644
  211. --- a/src/platforms/common/blackpill-f4/usbdfu.c
  212. +++ b/src/platforms/common/blackpill-f4/usbdfu.c
  213. @@ -73,6 +73,12 @@ int main(void)
  214.     dfu_protect(false);
  215.     dfu_init(&USB_DRIVER);
  216.  
  217. +#if 0
  218. +   /* https://github.com/libopencm3/libopencm3/pull/1256#issuecomment-779424001 */
  219. +   OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS | OTG_GCCFG_PWRDWN;
  220. +   OTG_FS_GCCFG &= ~(OTG_GCCFG_VBUSBSEN | OTG_GCCFG_VBUSASEN);
  221. +#endif
  222. +
  223.     dfu_main();
  224.  }
  225.  
  226. --
  227. 2.34.1
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement