main_f3.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * STM32F3 board support for the bootloader.
  3. *
  4. */
  5. #include "hw_config.h"
  6. #include <stdlib.h>
  7. #include <libopencm3/stm32/rcc.h>
  8. #include <libopencm3/stm32/gpio.h>
  9. #include <libopencm3/stm32/desig.h>
  10. #include <libopencm3/stm32/flash.h>
  11. #include <libopencm3/stm32/usart.h>
  12. #include <libopencm3/stm32/pwr.h>
  13. #include <libopencm3/cm3/systick.h>
  14. #include "bl.h"
  15. #define UDID_START 0x1FFFF7E8
  16. // address of MCU IDCODE
  17. #define DBGMCU_IDCODE 0xE0042000
  18. #define FLASH_BASE (0x08000000U)
  19. #define BOOT_RTC_REG MMIO32(RTC_BASE + 0x50)
  20. #ifdef INTERFACE_USART
  21. # define BOARD_INTERFACE_CONFIG (void *)BOARD_USART
  22. #else
  23. # define BOARD_INTERFACE_CONFIG NULL
  24. #endif
  25. const struct rcc_clock_scale _rcc_hsi_8mhz = {
  26. .pllsrc = RCC_CFGR_PLLSRC_HSI_DIV2,
  27. .pllmul = RCC_CFGR_PLLMUL_MUL16,
  28. .plldiv = RCC_CFGR2_PREDIV_NODIV,
  29. .hpre = RCC_CFGR_HPRE_DIV_NONE,
  30. .ppre1 = RCC_CFGR_PPRE1_DIV_2,
  31. .ppre2 = RCC_CFGR_PPRE2_DIV_NONE,
  32. .flash_waitstates = 2,
  33. .ahb_frequency = 64000000,
  34. .apb1_frequency = 32000000,
  35. .apb2_frequency = 64000000,
  36. };
  37. /* board definition */
  38. struct boardinfo board_info = {
  39. .board_type = BOARD_TYPE,
  40. .board_rev = 0,
  41. .fw_size = APP_SIZE_MAX,
  42. .systick_mhz = OSC_FREQ,
  43. };
  44. static void board_init(void);
  45. uint32_t
  46. board_get_devices(void)
  47. {
  48. return BOOT_DEVICES_SELECTION;
  49. }
  50. static void
  51. board_init(void)
  52. {
  53. /* initialise LEDs */
  54. rcc_peripheral_enable_clock(&BOARD_CLOCK_LEDS_REGISTER, BOARD_CLOCK_LEDS);
  55. gpio_mode_setup(
  56. BOARD_PORT_LEDS,
  57. GPIO_MODE_OUTPUT,
  58. GPIO_PUPD_NONE,
  59. BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
  60. gpio_set_output_options(
  61. BOARD_PORT_LEDS,
  62. GPIO_OTYPE_PP,
  63. GPIO_OSPEED_2MHZ,
  64. BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
  65. BOARD_LED_ON(
  66. BOARD_PORT_LEDS,
  67. BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
  68. /* if we have one, enable the force-bootloader pin */
  69. #ifdef BOARD_FORCE_BL_PIN
  70. rcc_peripheral_enable_clock(&BOARD_FORCE_BL_CLOCK_REGISTER, BOARD_FORCE_BL_CLOCK_BIT);
  71. gpio_mode_setup(BOARD_FORCE_BL_PORT,
  72. GPIO_MODE_INPUT,
  73. BOARD_FORCE_BL_PULL,
  74. BOARD_FORCE_BL_PIN);
  75. #endif
  76. /* enable the backup registers */
  77. rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN);
  78. #ifdef INTERFACE_USART
  79. /* configure usart pins */
  80. rcc_peripheral_enable_clock(&BOARD_USART_PIN_CLOCK_REGISTER, BOARD_USART_PIN_CLOCK_BIT_TX);
  81. rcc_peripheral_enable_clock(&BOARD_USART_PIN_CLOCK_REGISTER, BOARD_USART_PIN_CLOCK_BIT_RX);
  82. /* Setup GPIO pins for USART transmit. */
  83. gpio_mode_setup(BOARD_PORT_USART_TX, GPIO_MODE_AF, GPIO_PUPD_PULLUP, BOARD_PIN_TX);
  84. gpio_mode_setup(BOARD_PORT_USART_RX, GPIO_MODE_AF, GPIO_PUPD_PULLUP, BOARD_PIN_RX);
  85. /* Setup USART TX & RX pins as alternate function. */
  86. gpio_set_af(BOARD_PORT_USART_TX, BOARD_PORT_USART_AF, BOARD_PIN_TX);
  87. gpio_set_af(BOARD_PORT_USART_RX, BOARD_PORT_USART_AF, BOARD_PIN_RX);
  88. /* configure USART clock */
  89. rcc_peripheral_enable_clock(&BOARD_USART_CLOCK_REGISTER, BOARD_USART_CLOCK_BIT);
  90. #endif
  91. #ifdef INTERFACE_I2C
  92. # error I2C GPIO config not handled yet
  93. #endif
  94. }
  95. void
  96. board_deinit(void)
  97. {
  98. /* deinitialise LEDs */
  99. gpio_mode_setup(BOARD_PORT_LEDS,
  100. GPIO_MODE_INPUT,
  101. GPIO_PUPD_NONE,
  102. BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
  103. /* if we have one, disable the force-bootloader pin */
  104. #ifdef BOARD_FORCE_BL_PIN
  105. gpio_mode_setup(BOARD_FORCE_BL_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_FORCE_BL_PIN);
  106. #endif
  107. /* disable the backup registers */
  108. rcc_peripheral_disable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN);
  109. #ifdef INTERFACE_USART
  110. /* deinitialise GPIO pins for USART transmit. */
  111. gpio_mode_setup(BOARD_PORT_USART_TX, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_PIN_TX);
  112. gpio_mode_setup(BOARD_PORT_USART_RX, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_PIN_RX);
  113. /* disable USART peripheral clock */
  114. rcc_peripheral_disable_clock(&BOARD_USART_CLOCK_REGISTER, BOARD_USART_CLOCK_BIT);
  115. #endif
  116. #ifdef INTERFACE_I2C
  117. # error I2C GPIO config not handled yet
  118. #endif
  119. /* reset the APB2 peripheral clocks */
  120. RCC_AHBENR = 0x00000014; // XXX Magic reset number from STM32F3x reference manual
  121. }
  122. /**
  123. * @brief Initializes the RCC clock configuration.
  124. *
  125. * @param clock_setup : The clock configuration to set
  126. */
  127. static inline void
  128. clock_init(void)
  129. {
  130. rcc_clock_setup_hsi(&_rcc_hsi_8mhz);
  131. }
  132. /**
  133. * @brief Resets the RCC clock configuration to the default reset state.
  134. * @note The default reset state of the clock configuration is given below:
  135. * - HSI ON and used as system clock source
  136. * - HSE, PLL and PLLI2S OFF
  137. * - AHB, APB1 and APB2 prescaler set to 1.
  138. * - CSS, MCO1 and MCO2 OFF
  139. * - All interrupts disabled
  140. * @note This function doesn't modify the configuration of the
  141. * - Peripheral clocks
  142. * - LSI, LSE and RTC clocks
  143. */
  144. void
  145. clock_deinit(void)
  146. {
  147. /* Enable internal high-speed oscillator. */
  148. rcc_osc_on(RCC_HSI);
  149. rcc_wait_for_osc_ready(RCC_HSI);
  150. /* Reset the RCC_CFGR register */
  151. RCC_CFGR = 0x000000;
  152. /* Stop the HSE, CSS, PLL, PLLI2S, PLLSAI */
  153. rcc_osc_off(RCC_HSE);
  154. rcc_osc_off(RCC_PLL);
  155. rcc_css_disable();
  156. /* Reset the HSEBYP bit */
  157. rcc_osc_bypass_disable(RCC_HSE);
  158. /* Reset the CIR register */
  159. RCC_CIR = 0x000000;
  160. }
  161. /*---------------------------------------------------------------------------*/
  162. /** @brief Program a Half Word to FLASH
  163. This performs all operations necessary to program a 16 bit word to FLASH memory.
  164. The program error flag should be checked separately for the event that memory
  165. was not properly erased.
  166. Status bit polling is used to detect end of operation.
  167. @param[in] address Full address of flash half word to be programmed.
  168. @param[in] data half word to write
  169. */
  170. void flash_program_half_word(uint32_t address, uint16_t data)
  171. {
  172. flash_wait_for_last_operation();
  173. FLASH_CR |= FLASH_CR_PG;
  174. MMIO16(address) = data;
  175. flash_wait_for_last_operation();
  176. FLASH_CR &= ~FLASH_CR_PG;
  177. }
  178. /*---------------------------------------------------------------------------*/
  179. /** @brief Program a 32 bit Word to FLASH
  180. This performs all operations necessary to program a 32 bit word to FLASH memory.
  181. The program error flag should be checked separately for the event that memory
  182. was not properly erased.
  183. Status bit polling is used to detect end of operation.
  184. @param[in] address Full address of flash word to be programmed.
  185. @param[in] data word to write
  186. */
  187. void flash_program_word(uint32_t address, uint32_t data)
  188. {
  189. flash_program_half_word(address, (uint16_t)data);
  190. flash_program_half_word(address + 2, (uint16_t)(data >> 16));
  191. }
  192. /*---------------------------------------------------------------------------*/
  193. /** @brief Erase a Page of FLASH
  194. This performs all operations necessary to erase a page in FLASH memory.
  195. The page should be checked to ensure that it was properly erased. A page must
  196. first be fully erased before attempting to program it.
  197. Note that the page sizes differ between devices. See the reference manual or
  198. the FLASH programming manual for details.
  199. @param[in] page_address Full address of flash page to be erased.
  200. */
  201. void flash_erase_page(uint32_t page_address)
  202. {
  203. flash_wait_for_last_operation();
  204. FLASH_CR |= FLASH_CR_PER;
  205. FLASH_AR = page_address;
  206. FLASH_CR |= FLASH_CR_STRT;
  207. flash_wait_for_last_operation();
  208. FLASH_CR &= ~FLASH_CR_PER;
  209. }
  210. uint32_t
  211. flash_func_sector_size(unsigned sector)
  212. {
  213. if (sector < BOARD_FLASH_SECTORS) {
  214. return FLASH_SECTOR_SIZE;
  215. }
  216. return 0;
  217. }
  218. void
  219. flash_func_erase_sector(unsigned sector)
  220. {
  221. if (sector < BOARD_FLASH_SECTORS) {
  222. flash_erase_page(APP_LOAD_ADDRESS + (sector * FLASH_SECTOR_SIZE));
  223. }
  224. }
  225. void
  226. flash_func_write_word(uint32_t address, uint32_t word)
  227. {
  228. flash_program_word(address + APP_LOAD_ADDRESS, word);
  229. }
  230. uint32_t
  231. flash_func_read_word(uint32_t address)
  232. {
  233. return *(uint32_t *)(address + APP_LOAD_ADDRESS);
  234. }
  235. uint32_t
  236. flash_func_read_otp(uint32_t address)
  237. {
  238. return 0;
  239. }
  240. uint32_t get_mcu_id(void)
  241. {
  242. return *(uint32_t *)DBGMCU_IDCODE;
  243. }
  244. // See F4 version for future enhancement for full decoding
  245. int get_mcu_desc(int max, uint8_t *revstr)
  246. {
  247. const char none[] = "STM32F3xxx,?";
  248. int i;
  249. for (i = 0; none[i] && i < max - 1; i++) {
  250. revstr[i] = none[i];
  251. }
  252. return i;
  253. }
  254. int check_silicon(void)
  255. {
  256. return 0;
  257. }
  258. uint32_t
  259. flash_func_read_sn(uint32_t address)
  260. {
  261. // read a byte out from unique chip ID area
  262. // it's 12 bytes, or 3 words.
  263. return *(uint32_t *)(address + UDID_START);
  264. }
  265. void
  266. led_on(unsigned led)
  267. {
  268. switch (led) {
  269. case LED_ACTIVITY:
  270. BOARD_LED_ON(BOARD_PORT_LEDS, BOARD_PIN_LED_ACTIVITY);
  271. break;
  272. case LED_BOOTLOADER:
  273. BOARD_LED_ON(BOARD_PORT_LEDS, BOARD_PIN_LED_BOOTLOADER);
  274. break;
  275. }
  276. }
  277. void
  278. led_off(unsigned led)
  279. {
  280. switch (led) {
  281. case LED_ACTIVITY:
  282. BOARD_LED_OFF(BOARD_PORT_LEDS, BOARD_PIN_LED_ACTIVITY);
  283. break;
  284. case LED_BOOTLOADER:
  285. BOARD_LED_OFF(BOARD_PORT_LEDS, BOARD_PIN_LED_BOOTLOADER);
  286. break;
  287. }
  288. }
  289. void
  290. led_toggle(unsigned led)
  291. {
  292. switch (led) {
  293. case LED_ACTIVITY:
  294. gpio_toggle(BOARD_PORT_LEDS, BOARD_PIN_LED_ACTIVITY);
  295. break;
  296. case LED_BOOTLOADER:
  297. gpio_toggle(BOARD_PORT_LEDS, BOARD_PIN_LED_BOOTLOADER);
  298. break;
  299. }
  300. }
  301. static bool
  302. should_wait(void)
  303. {
  304. bool result = false;
  305. PWR_CR |= PWR_CR_DBP;
  306. if (BOOT_RTC_REG == BL_WAIT_MAGIC) {
  307. result = true;
  308. BOOT_RTC_REG = 0;
  309. }
  310. PWR_CR &= ~PWR_CR_DBP;
  311. return result;
  312. }
  313. int
  314. main(void)
  315. {
  316. unsigned timeout = 0;
  317. /* do board-specific initialisation */
  318. board_init();
  319. #if defined(INTERFACE_USART) | defined (INTERFACE_USB)
  320. /* XXX sniff for a USART connection to decide whether to wait in the bootloader? */
  321. timeout = BOOTLOADER_DELAY;
  322. #endif
  323. #ifdef INTERFACE_I2C
  324. # error I2C bootloader detection logic not implemented
  325. #endif
  326. /* if the app left a cookie saying we should wait, then wait */
  327. if (should_wait()) {
  328. timeout = BOOTLOADER_DELAY;
  329. }
  330. #ifdef BOARD_FORCE_BL_PIN
  331. /* if the force-BL pin state matches the state of the pin, wait in the bootloader forever */
  332. if (BOARD_FORCE_BL_VALUE == gpio_get(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN)) {
  333. timeout = 0xffffffff;
  334. }
  335. #endif
  336. /* look for the magic wait-in-bootloader value in backup register zero */
  337. /* if we aren't expected to wait in the bootloader, try to boot immediately */
  338. if (timeout == 0) {
  339. /* try to boot immediately */
  340. jump_to_app();
  341. /* if we returned, there is no app; go to the bootloader and stay there */
  342. timeout = 0;
  343. }
  344. /* configure the clock for bootloader activity */
  345. clock_init();
  346. /* start the interface */
  347. cinit(BOARD_INTERFACE_CONFIG, USART);
  348. while (1) {
  349. /* run the bootloader, possibly coming back after the timeout */
  350. bootloader(timeout);
  351. /* look to see if we can boot the app */
  352. jump_to_app();
  353. /* boot failed; stay in the bootloader forever next time */
  354. timeout = 0;
  355. }
  356. }