CuVoodoo STM32F1 firmware template
bootloader.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
20 /* standard libraries */
21 #include <stdint.h> // standard integer types
22 #include <stdbool.h> // boolean types
23 
24 /* STM32 (including CM3) libraries */
25 #include <libopencm3/cm3/scb.h> // vector table definition
26 #include <libopencm3/stm32/rcc.h> // clock utilities
27 #include <libopencm3/stm32/gpio.h> // GPIO utilities
28 
29 /* own libraries */
30 #include "global.h" // board definitions
31 #include "usb_dfu.h" // USB DFU utilities
32 
34 void main(void);
35 void main(void)
36 {
37  // check of DFU mode is forced
38  bool dfu_force = false; // to remember if DFU mode is forced
39  // check if a soft boot has been used
40  if (0==(RCC_CSR&0xfc000000)) { // no reset flag present -> this was a soft reset using scb_reset_core() after clearing the flags using RCC_CSR_RMVF, very probably to start the DFU mode
41  dfu_force = true;
42  } else { // check if the force DFU mode input is set
43  // disable SWJ pin to use as GPIO
44 #if (GPIO(B)==GPIO(DFU_FORCE_PORT)) && (GPIO(4)==GPIO(DFU_FORCE_PIN))
45  gpio_primary_remap(AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST, 0);
46 #elif ((GPIO(B)==GPIO(DFU_FORCE_PORT)) && (GPIO(3)==GPIO(DFU_FORCE_PIN))) || ((GPIO(A)==GPIO(DFU_FORCE_PORT)) && (GPIO(15)==GPIO(DFU_FORCE_PIN)))
47  gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 0);
48 #elif ((GPIO(A)==GPIO(DFU_FORCE_PORT)) && (GPIO(14)==GPIO(DFU_FORCE_PIN))) || ((GPIO(A)==GPIO(DFU_FORCE_PORT)) && (GPIO(13)==GPIO(DFU_FORCE_PIN)))
49  gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF, 0);
50 #endif
51  rcc_periph_clock_enable(RCC_GPIO(DFU_FORCE_PORT)); // enable clock for GPIO domain
52  gpio_set_mode(GPIO(DFU_FORCE_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(DFU_FORCE_PIN)); // set GPIO to input
53  // pull on the opposite of the expected value
54 #if (DFU_FORCE_VALUE==1)
55  gpio_clear(GPIO(DFU_FORCE_PORT), GPIO(DFU_FORCE_PIN)); // pull down to be able to detect when tied to high
56  if (gpio_get(GPIO(DFU_FORCE_PORT), GPIO(DFU_FORCE_PIN))) { // check if output is set to the value to force DFU mode
57 #else
58  gpio_set(GPIO(DFU_FORCE_PORT), GPIO(DFU_FORCE_PIN)); // pull up to be able to detect when tied to low
59  if (0==gpio_get(GPIO(DFU_FORCE_PORT), GPIO(DFU_FORCE_PIN))) { // check if output is set to the value to force DFU mode
60 #endif
61  dfu_force = true; // DFU mode forced
62  }
63  }
64 
65  // start application if valid
66  /* the application starts with the vector table
67  * the first entry in the vector table is the initial stack pointer (SP) address
68  * the stack will be placed in RAM
69  * on STM32F1xx SRAM begins at 0x2000 0000, and on STM32F103x8 there is 20KB of RAM (0x5000).
70  * since the stack grown "downwards" it should start at the end of the RAM: 0x2000 5000
71  * if the SP is not in this range (e.g. flash has been erased) there is no valid application
72  * the second entry in the vector table is the reset address, corresponding to the application start
73  */
74  volatile uint32_t* application = &__application_beginning; // get the value of the application address symbol (use a register instead on the stack since the stack pointer will be changed)
75  if (!dfu_force && (((*application)&0xFFFE0000)==0x20000000)) { // application at address seems valid
76  SCB_VTOR = (volatile uint32_t)(application); // set vector table to application vector table (store at the beginning of the application)
77  __asm__ volatile ("MSR msp,%0" : :"r"(*application)); // set stack pointer to address provided in the beginning of the application (loaded into a register first)
78  (*(void(**)(void))(application + 1))(); // start application (by jumping to the reset function which address is stored as second entry of the vector table)
79  }
80 
81  rcc_clock_setup_in_hse_8mhz_out_72mhz(); // start main clock
82  board_setup(); // setup board to control LED
83 #if defined(BUSVOODOO)
84  led_red(); // switch red LED to indicate bootloader started
85 #else
86  led_on(); // indicate bootloader started
87 #endif
88  usb_dfu_setup(); // setup USB DFU for firmware upload
89  usb_dfu_start(); // run DFU mode
90 }
void board_setup(void)
setup board peripherals
Definition: global.c:211
void main(void)
bootloader entry point
Definition: bootloader.c:35
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
#define DFU_FORCE_PORT
JNTRST port (needs to be remapped to become PB4)
Definition: global.h:416
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
void usb_dfu_start(void)
start USB DFU handling
Definition: usb_dfu.c:297
void usb_dfu_setup(void)
setup USB DFU peripheral
Definition: usb_dfu.c:286
#define DFU_FORCE_PIN
JNTRST pin (needs to be remapped to become PB4)
Definition: global.h:417
library for USB DFU to write on internal flash (API)
uint32_t __application_beginning
symbol for beginning of the application
void led_on(void)
switch on board LED
Definition: global.c:68