| 1 |
// The code is this file allows the user to enter DFU mode when the board |
| 2 |
// starts up, by connecting POS10 on the external connector to GND. |
| 3 |
// The code itself is taken from the LimiFrog software repository found at |
| 4 |
// https://github.com/LimiFrog/LimiFrog-SW, and the original license header |
| 5 |
// is copied below. |
| 6 |
|
| 7 |
#include STM32_HAL_H |
| 8 |
|
| 9 |
static void LBF_DFU_If_Needed(void); |
| 10 |
|
| 11 |
void LIMIFROG_board_early_init(void) { |
| 12 |
LBF_DFU_If_Needed(); |
| 13 |
} |
| 14 |
|
| 15 |
/******************************************************************************* |
| 16 |
* LBF_DFU_If_Needed.c |
| 17 |
* |
| 18 |
* (c)2015 LimiFrog / CYMEYA |
| 19 |
* This program is licensed under the terms of the MIT License. |
| 20 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. |
| 21 |
* Please refer to the License File LICENSE.txt located at the root of this |
| 22 |
* project for full licensing conditions, |
| 23 |
* or visit https://opensource.org/licenses/MIT. |
| 24 |
******************************************************************************/ |
| 25 |
|
| 26 |
#define __LIMIFROG_02 |
| 27 |
|
| 28 |
/* ==== BTLE (excl UART) ======================================== */ |
| 29 |
// PC9 = BT_RST (active high) |
| 30 |
|
| 31 |
#define BT_RST_PIN GPIO_PIN_9 |
| 32 |
#define BT_RST_PORT GPIOC |
| 33 |
|
| 34 |
// Position 10 |
| 35 |
#ifdef __LIMIFROG_01 |
| 36 |
#define CONN_POS10_PIN GPIO_PIN_9 |
| 37 |
#define CONN_POS10_PORT GPIOB |
| 38 |
#else |
| 39 |
#define CONN_POS10_PIN GPIO_PIN_8 |
| 40 |
#define CONN_POS10_PORT GPIOB |
| 41 |
#endif |
| 42 |
|
| 43 |
static inline void GPIO_HIGH(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) { |
| 44 |
GPIOx->BSRR = (uint32_t)GPIO_Pin; |
| 45 |
} |
| 46 |
|
| 47 |
static inline int IS_GPIO_RESET(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) { |
| 48 |
GPIO_PinState bitstatus; |
| 49 |
if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET) { |
| 50 |
bitstatus = GPIO_PIN_SET; |
| 51 |
} else { |
| 52 |
bitstatus = GPIO_PIN_RESET; |
| 53 |
} |
| 54 |
return bitstatus == GPIO_PIN_RESET; |
| 55 |
} |
| 56 |
|
| 57 |
/************************************************************** |
| 58 |
RATIONALE FOR THIS FUNCTION : |
| 59 |
|
| 60 |
- The STM32 embeds in ROM a bootloader that allows to |
| 61 |
obtain code and boot from a number of different interfaces, |
| 62 |
including USB in a mode called "DFU" (Device Firmware Update) |
| 63 |
[see AN3606 from ST for full details] |
| 64 |
This bootloader code is executed instead of the regular |
| 65 |
application code when pin BOOT0 is pulled-up (which on |
| 66 |
LimiFrog0.2 is achieved by pressing the general-purpose |
| 67 |
pushbutton switch on the side. |
| 68 |
- The bootloader monitors a number of IOs of the STM32 to decide |
| 69 |
from which interface it should boot. |
| 70 |
- Problem in LimiFrog (up to versions 0.2a at least): upon |
| 71 |
power-up the BLE modules generates some activity on UART3, |
| 72 |
which is part of the pins monitored by the STM32. |
| 73 |
This misleads the bootloader in trying to boot from UART3 |
| 74 |
and, as a result, not continuing with booting from USB. |
| 75 |
|
| 76 |
- This code implements an alternative solution to launch the |
| 77 |
bootloader while making sure UART3 remains stable. |
| 78 |
- The idea it to start application code with a check, prior to any |
| 79 |
other applicative code, of whether USB bootload is required (as |
| 80 |
flagged by a GPIO pulled low at reset, in the same way as BOOT0). |
| 81 |
The hadware reset pin of BLE is asserted (so that now it won't |
| 82 |
generate any activity on UART3), and if USB bootload is required : |
| 83 |
bootload ROM is remapped at address 0x0, stack pointer is |
| 84 |
updated and the code is branched to the start of the bootloader. |
| 85 |
- This code is run prior to any applicative configuration of clocks, |
| 86 |
IRQs etc. -- the STM32 is therefore still running from MSI |
| 87 |
|
| 88 |
THIS FUNCTION MAY BE SUPPRESSED IF YOU NEVER NEED TO BOOT DFU MODE |
| 89 |
|
| 90 |
********************************************************************/ |
| 91 |
|
| 92 |
static void LBF_DFU_If_Needed(void) { |
| 93 |
|
| 94 |
|
| 95 |
GPIO_InitTypeDef GPIO_InitStruct; |
| 96 |
|
| 97 |
|
| 98 |
// Initialize and assert pin BTLE_RST |
| 99 |
// (hw reset to BLE module, so it won't drive UART3) |
| 100 |
|
| 101 |
__HAL_RCC_GPIOC_CLK_ENABLE(); |
| 102 |
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 103 |
GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 104 |
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 105 |
GPIO_InitStruct.Pin = BT_RST_PIN; |
| 106 |
HAL_GPIO_Init(BT_RST_PORT, &GPIO_InitStruct); |
| 107 |
|
| 108 |
GPIO_HIGH(BT_RST_PORT, BT_RST_PIN); // assert BTLE reset |
| 109 |
|
| 110 |
|
| 111 |
/* -- Bootloader will be called if position 10 on the extension port |
| 112 |
is actively pulled low -- */ |
| 113 |
// Note - this is an arbitrary choice, code could be modified to |
| 114 |
// monitor another GPIO of the STM32 and/or decide that active level |
| 115 |
// is high rather than low |
| 116 |
|
| 117 |
|
| 118 |
// Initialize Extension Port Position 10 = PB8 (bears I2C1_SCL) |
| 119 |
// Use weak pull-up to detect if pin is externally pulled low |
| 120 |
|
| 121 |
__HAL_RCC_GPIOB_CLK_ENABLE(); |
| 122 |
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 123 |
GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 124 |
GPIO_InitStruct.Pin = CONN_POS10_PIN; |
| 125 |
HAL_GPIO_Init(CONN_POS10_PORT, &GPIO_InitStruct); |
| 126 |
|
| 127 |
// If selection pin pulled low... |
| 128 |
if (IS_GPIO_RESET(CONN_POS10_PORT, CONN_POS10_PIN)) { |
| 129 |
// Remap bootloader ROM (ie System Flash) to address 0x0 |
| 130 |
SYSCFG->MEMRMP = 0x00000001; |
| 131 |
|
| 132 |
// Init stack pointer with value residing at ROM base |
| 133 |
asm ( |
| 134 |
"LDR R0, =0x00000000\n\t" // load ROM base address" |
| 135 |
"LDR SP,[R0, #0]\n\t" // assign main stack pointer" |
| 136 |
); |
| 137 |
|
| 138 |
// Jump to address pointed by 0x00000004 -- */ |
| 139 |
|
| 140 |
asm ( |
| 141 |
"LDR R0,[R0, #4]\n\t" // load bootloader address |
| 142 |
"BX R0\n\t" |
| 143 |
); |
| 144 |
|
| 145 |
} |
| 146 |
} |