/* bootloader support functions */ #include #include "ch.h" #include "hal.h" #include "hwdef.h" #include #include #include #include "support.h" #include "mcu_f4.h" #include "mcu_f7.h" static BaseChannel *uarts[] = { BOOTLOADER_DEV_LIST }; #if HAL_USE_SERIAL == TRUE static SerialConfig sercfg; #endif static int8_t locked_uart = -1; static uint8_t last_uart; #ifndef BOOTLOADER_BAUDRATE #define BOOTLOADER_BAUDRATE 115200 #endif int16_t cin(unsigned timeout_ms) { uint8_t b = 0; for (uint8_t i=0; i= flash_base_page+num_pages) { return 0; } return stm32_flash_getpagesize(flash_base_page+sector); } void flash_func_erase_sector(uint32_t sector) { if (!stm32_flash_ispageerased(flash_base_page+sector)) { stm32_flash_erasepage(flash_base_page+sector); } } // read one-time programmable memory uint32_t flash_func_read_otp(uint32_t idx) { if (idx & 3) { return 0; } if (idx > OTP_SIZE) { return 0; } return *(uint32_t *)(idx + OTP_BASE); } // read chip serial number uint32_t flash_func_read_sn(uint32_t idx) { return *(uint32_t *)(UDID_START + idx); } uint32_t get_mcu_id(void) { return *(uint32_t *)DBGMCU_BASE; } #define REVID_MASK 0xFFFF0000 #define DEVID_MASK 0xFFF uint32_t get_mcu_desc(uint32_t max, uint8_t *revstr) { uint32_t idcode = (*(uint32_t *)DBGMCU_BASE); int32_t mcuid = idcode & DEVID_MASK; uint16_t revid = ((idcode & REVID_MASK) >> 16); mcu_des_t des = mcu_descriptions[STM32_UNKNOWN]; for (int i = 0; i < ARRAY_SIZE_SIMPLE(mcu_descriptions); i++) { if (mcuid == mcu_descriptions[i].mcuid) { des = mcu_descriptions[i]; break; } } for (int i = 0; i < ARRAY_SIZE_SIMPLE(silicon_revs); i++) { if (silicon_revs[i].revid == revid) { des.rev = silicon_revs[i].rev; } } uint8_t *endp = &revstr[max - 1]; uint8_t *strp = revstr; while (strp < endp && *des.desc) { *strp++ = *des.desc++; } if (strp < endp) { *strp++ = ','; } if (strp < endp) { *strp++ = des.rev; } return strp - revstr; } /* see if we should limit flash to 1M on devices with older revisions */ bool check_limit_flash_1M(void) { uint32_t idcode = (*(uint32_t *)DBGMCU_BASE); uint16_t revid = ((idcode & REVID_MASK) >> 16); for (int i = 0; i < ARRAY_SIZE_SIMPLE(silicon_revs); i++) { if (silicon_revs[i].revid == revid) { return silicon_revs[i].limit_flash_size_1M; } } return false; } void led_on(unsigned led) { #ifdef HAL_GPIO_PIN_LED_BOOTLOADER if (led == LED_BOOTLOADER) { palWriteLine(HAL_GPIO_PIN_LED_BOOTLOADER, HAL_LED_ON); } #endif #ifdef HAL_GPIO_PIN_LED_ACTIVITY if (led == LED_ACTIVITY) { palWriteLine(HAL_GPIO_PIN_LED_ACTIVITY, HAL_LED_ON); } #endif } void led_off(unsigned led) { #ifdef HAL_GPIO_PIN_LED_BOOTLOADER if (led == LED_BOOTLOADER) { palWriteLine(HAL_GPIO_PIN_LED_BOOTLOADER, !HAL_LED_ON); } #endif #ifdef HAL_GPIO_PIN_LED_ACTIVITY if (led == LED_ACTIVITY) { palWriteLine(HAL_GPIO_PIN_LED_ACTIVITY, !HAL_LED_ON); } #endif } void led_toggle(unsigned led) { #ifdef HAL_GPIO_PIN_LED_BOOTLOADER if (led == LED_BOOTLOADER) { palToggleLine(HAL_GPIO_PIN_LED_BOOTLOADER); } #endif #ifdef HAL_GPIO_PIN_LED_ACTIVITY if (led == LED_ACTIVITY) { palToggleLine(HAL_GPIO_PIN_LED_ACTIVITY); } #endif } extern "C" { int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); } // printf to USB for debugging void uprintf(const char *fmt, ...) { char msg[200]; va_list ap; va_start(ap, fmt); uint32_t n = vsnprintf(msg, sizeof(msg), fmt, ap); va_end(ap); chnWriteTimeout(&SDU1, (const uint8_t *)msg, n, MS2ST(100)); } // generate a pulse sequence forever, for debugging void led_pulses(uint8_t npulses) { led_off(LED_BOOTLOADER); while (true) { for (uint8_t i=0; i