2018-03-28 21:45:13 -03:00
|
|
|
/*
|
|
|
|
bootloader support functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "hwdef.h"
|
|
|
|
#include <AP_HAL_ChibiOS/hwdef/common/usbcfg.h>
|
|
|
|
#include <AP_HAL_ChibiOS/hwdef/common/flash.h>
|
2018-06-21 03:41:07 -03:00
|
|
|
#include <AP_HAL_ChibiOS/hwdef/common/stm32_util.h>
|
2020-07-14 18:31:14 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2018-03-28 21:45:13 -03:00
|
|
|
#include "support.h"
|
2019-05-26 22:38:46 -03:00
|
|
|
#include "mcu_f1.h"
|
2019-10-31 08:00:27 -03:00
|
|
|
#include "mcu_f3.h"
|
2018-06-21 03:41:07 -03:00
|
|
|
#include "mcu_f4.h"
|
|
|
|
#include "mcu_f7.h"
|
2019-02-03 05:23:56 -04:00
|
|
|
#include "mcu_h7.h"
|
2021-03-09 02:44:37 -04:00
|
|
|
#include "mcu_g4.h"
|
2021-09-19 03:37:50 -03:00
|
|
|
#include "mcu_l4.h"
|
2018-03-28 21:45:13 -03:00
|
|
|
|
2019-05-26 22:38:46 -03:00
|
|
|
// optional uprintf() code for debug
|
|
|
|
// #define BOOTLOADER_DEBUG SD1
|
|
|
|
|
|
|
|
#if defined(BOOTLOADER_DEV_LIST)
|
2018-06-25 00:39:22 -03:00
|
|
|
static BaseChannel *uarts[] = { BOOTLOADER_DEV_LIST };
|
2018-06-25 03:08:51 -03:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-06-25 00:39:22 -03:00
|
|
|
static SerialConfig sercfg;
|
2018-06-25 03:08:51 -03:00
|
|
|
#endif
|
2018-06-25 00:39:22 -03:00
|
|
|
static int8_t locked_uart = -1;
|
|
|
|
static uint8_t last_uart;
|
|
|
|
|
2018-06-25 03:08:51 -03:00
|
|
|
#ifndef BOOTLOADER_BAUDRATE
|
|
|
|
#define BOOTLOADER_BAUDRATE 115200
|
|
|
|
#endif
|
|
|
|
|
2023-07-09 05:10:18 -03:00
|
|
|
#ifndef AP_BOOTLOADER_ALWAYS_ERASE
|
|
|
|
#define AP_BOOTLOADER_ALWAYS_ERASE 0
|
|
|
|
#endif
|
|
|
|
|
2019-02-08 01:37:03 -04:00
|
|
|
// #pragma GCC optimize("O0")
|
|
|
|
|
2021-03-16 03:20:49 -03:00
|
|
|
static bool cin_data(uint8_t *data, uint8_t len, unsigned timeout_ms)
|
2018-03-28 21:45:13 -03:00
|
|
|
{
|
2018-08-02 20:27:17 -03:00
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(uarts); i++) {
|
2018-06-25 00:39:22 -03:00
|
|
|
if (locked_uart == -1 || locked_uart == i) {
|
2021-03-16 03:20:49 -03:00
|
|
|
if (chnReadTimeout(uarts[i], data, len, chTimeMS2I(timeout_ms)) == len) {
|
2018-06-25 00:39:22 -03:00
|
|
|
last_uart = i;
|
2021-03-16 03:20:49 -03:00
|
|
|
return true;
|
2018-06-25 00:39:22 -03:00
|
|
|
}
|
|
|
|
}
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
2018-06-25 01:10:21 -03:00
|
|
|
chThdSleepMicroseconds(500);
|
2021-03-16 03:20:49 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t cin(unsigned timeout_ms)
|
|
|
|
{
|
|
|
|
uint8_t b = 0;
|
|
|
|
if (cin_data(&b, 1, timeout_ms)) {
|
|
|
|
return b;
|
|
|
|
}
|
2018-06-25 00:39:22 -03:00
|
|
|
return -1;
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2018-06-24 19:29:54 -03:00
|
|
|
int cin_word(uint32_t *wp, unsigned timeout_ms)
|
|
|
|
{
|
2021-03-16 03:20:49 -03:00
|
|
|
if (cin_data((uint8_t *)wp, 4, timeout_ms)) {
|
|
|
|
return 0;
|
2018-06-24 19:29:54 -03:00
|
|
|
}
|
2018-06-25 00:39:22 -03:00
|
|
|
return -1;
|
2018-06-24 19:29:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-28 21:45:13 -03:00
|
|
|
void cout(uint8_t *data, uint32_t len)
|
|
|
|
{
|
2018-08-02 04:02:55 -03:00
|
|
|
chnWriteTimeout(uarts[last_uart], data, len, chTimeMS2I(100));
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
2019-05-26 22:38:46 -03:00
|
|
|
#endif // BOOTLOADER_DEV_LIST
|
2018-03-28 21:45:13 -03:00
|
|
|
|
|
|
|
static uint32_t flash_base_page;
|
2021-03-09 02:44:37 -04:00
|
|
|
static uint16_t num_pages;
|
2020-08-23 17:02:56 -03:00
|
|
|
static const uint8_t *flash_base = (const uint8_t *)(0x08000000 + (FLASH_BOOTLOADER_LOAD_KB + APP_START_OFFSET_KB)*1024U);
|
2018-03-28 21:45:13 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
initialise flash_base_page and num_pages
|
|
|
|
*/
|
|
|
|
void flash_init(void)
|
|
|
|
{
|
|
|
|
uint32_t reserved = 0;
|
|
|
|
num_pages = stm32_flash_getnumpages();
|
2020-04-12 01:47:25 -03:00
|
|
|
/*
|
2020-08-23 17:02:56 -03:00
|
|
|
advance flash_base_page to account for (FLASH_BOOTLOADER_LOAD_KB + APP_START_OFFSET_KB)
|
2020-04-12 01:47:25 -03:00
|
|
|
*/
|
2020-08-23 17:02:56 -03:00
|
|
|
while (reserved < (FLASH_BOOTLOADER_LOAD_KB + APP_START_OFFSET_KB) * 1024U &&
|
2018-03-28 21:45:13 -03:00
|
|
|
flash_base_page < num_pages) {
|
|
|
|
reserved += stm32_flash_getpagesize(flash_base_page);
|
|
|
|
flash_base_page++;
|
|
|
|
}
|
2020-04-12 01:47:25 -03:00
|
|
|
/*
|
|
|
|
reduce num_pages to account for FLASH_RESERVE_END_KB
|
|
|
|
*/
|
|
|
|
reserved = 0;
|
|
|
|
while (reserved < FLASH_RESERVE_END_KB * 1024U) {
|
|
|
|
reserved += stm32_flash_getpagesize(num_pages-1);
|
|
|
|
num_pages--;
|
|
|
|
}
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2018-06-24 19:29:54 -03:00
|
|
|
void flash_set_keep_unlocked(bool set)
|
|
|
|
{
|
|
|
|
stm32_flash_keep_unlocked(set);
|
|
|
|
}
|
2018-03-28 21:45:13 -03:00
|
|
|
|
|
|
|
/*
|
2020-08-23 17:02:56 -03:00
|
|
|
read a word at offset relative to flash base
|
2018-03-28 21:45:13 -03:00
|
|
|
*/
|
2021-06-26 04:49:34 -03:00
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
|
2018-03-28 21:45:13 -03:00
|
|
|
uint32_t flash_func_read_word(uint32_t offset)
|
|
|
|
{
|
|
|
|
return *(const uint32_t *)(flash_base + offset);
|
|
|
|
}
|
2021-08-22 05:33:00 -03:00
|
|
|
#pragma GCC diagnostic pop
|
2018-03-28 21:45:13 -03:00
|
|
|
|
2019-02-03 21:42:16 -04:00
|
|
|
bool flash_func_write_word(uint32_t offset, uint32_t v)
|
2018-03-28 21:45:13 -03:00
|
|
|
{
|
2019-02-03 21:42:16 -04:00
|
|
|
return stm32_flash_write(uint32_t(flash_base+offset), &v, sizeof(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool flash_func_write_words(uint32_t offset, uint32_t *v, uint8_t n)
|
|
|
|
{
|
|
|
|
return stm32_flash_write(uint32_t(flash_base+offset), v, n*sizeof(*v));
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flash_func_sector_size(uint32_t sector)
|
|
|
|
{
|
2019-01-06 01:13:44 -04:00
|
|
|
if (sector >= num_pages-flash_base_page) {
|
2018-03-28 21:45:13 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return stm32_flash_getpagesize(flash_base_page+sector);
|
|
|
|
}
|
|
|
|
|
2023-07-31 03:26:43 -03:00
|
|
|
bool flash_func_is_erased(uint32_t sector)
|
|
|
|
{
|
|
|
|
return stm32_flash_ispageerased(flash_base_page+sector);
|
|
|
|
}
|
|
|
|
|
2022-10-06 21:14:38 -03:00
|
|
|
bool flash_func_erase_sector(uint32_t sector, bool force_erase)
|
2018-03-28 21:45:13 -03:00
|
|
|
{
|
2023-07-09 05:10:18 -03:00
|
|
|
#if AP_BOOTLOADER_ALWAYS_ERASE
|
|
|
|
return stm32_flash_erasepage(flash_base_page+sector);
|
|
|
|
#else
|
2022-10-06 21:14:38 -03:00
|
|
|
if (force_erase || !stm32_flash_ispageerased(flash_base_page+sector)) {
|
2019-02-06 17:11:32 -04:00
|
|
|
return stm32_flash_erasepage(flash_base_page+sector);
|
2018-06-24 21:33:28 -03:00
|
|
|
}
|
2019-02-06 17:11:32 -04:00
|
|
|
return true;
|
2023-07-09 05:10:18 -03:00
|
|
|
#endif
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2018-06-21 03:41:07 -03:00
|
|
|
// read one-time programmable memory
|
2018-03-28 21:45:13 -03:00
|
|
|
uint32_t flash_func_read_otp(uint32_t idx)
|
|
|
|
{
|
2019-02-03 05:23:56 -04:00
|
|
|
#ifndef OTP_SIZE
|
|
|
|
return 0;
|
|
|
|
#else
|
2018-06-21 03:41:07 -03:00
|
|
|
if (idx & 3) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx > OTP_SIZE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *(uint32_t *)(idx + OTP_BASE);
|
2019-02-03 05:23:56 -04:00
|
|
|
#endif
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2018-06-21 03:41:07 -03:00
|
|
|
// read chip serial number
|
2018-03-28 21:45:13 -03:00
|
|
|
uint32_t flash_func_read_sn(uint32_t idx)
|
|
|
|
{
|
2018-06-21 03:41:07 -03:00
|
|
|
return *(uint32_t *)(UDID_START + idx);
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2020-07-14 18:31:14 -03:00
|
|
|
/*
|
|
|
|
we use a write buffer for flashing, both for efficiency and to
|
|
|
|
ensure that we only ever do 32 byte aligned writes on STM32H7. If
|
|
|
|
you attempt to do writes on a H7 of less than 32 bytes or not
|
|
|
|
aligned then the flash can end up in a CRC error state, which can
|
|
|
|
generate a hardware fault (a double ECC error) on flash read, even
|
|
|
|
after a power cycle
|
|
|
|
*/
|
|
|
|
static struct {
|
|
|
|
uint32_t buffer[8];
|
|
|
|
uint32_t address;
|
|
|
|
uint8_t n;
|
|
|
|
} fbuf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
flush the write buffer
|
|
|
|
*/
|
|
|
|
bool flash_write_flush(void)
|
|
|
|
{
|
|
|
|
if (fbuf.n == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
fbuf.n = 0;
|
|
|
|
return flash_func_write_words(fbuf.address, fbuf.buffer, ARRAY_SIZE(fbuf.buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write to flash with buffering to 32 bytes alignment
|
|
|
|
*/
|
|
|
|
bool flash_write_buffer(uint32_t address, const uint32_t *v, uint8_t nwords)
|
|
|
|
{
|
|
|
|
if (fbuf.n > 0 && address != fbuf.address + fbuf.n*4) {
|
|
|
|
if (!flash_write_flush()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nwords > 0) {
|
|
|
|
if (fbuf.n == 0) {
|
|
|
|
fbuf.address = address;
|
|
|
|
memset(fbuf.buffer, 0xff, sizeof(fbuf.buffer));
|
|
|
|
}
|
|
|
|
uint8_t n = MIN(ARRAY_SIZE(fbuf.buffer)-fbuf.n, nwords);
|
|
|
|
memcpy(&fbuf.buffer[fbuf.n], v, n*4);
|
|
|
|
address += n*4;
|
|
|
|
v += n;
|
|
|
|
nwords -= n;
|
|
|
|
fbuf.n += n;
|
|
|
|
if (fbuf.n == ARRAY_SIZE(fbuf.buffer)) {
|
|
|
|
if (!flash_write_flush()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-28 21:45:13 -03:00
|
|
|
uint32_t get_mcu_id(void)
|
|
|
|
{
|
2018-06-21 03:41:07 -03:00
|
|
|
return *(uint32_t *)DBGMCU_BASE;
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2018-06-21 03:41:07 -03:00
|
|
|
#define REVID_MASK 0xFFFF0000
|
|
|
|
#define DEVID_MASK 0xFFF
|
|
|
|
|
|
|
|
uint32_t get_mcu_desc(uint32_t max, uint8_t *revstr)
|
2018-03-28 21:45:13 -03:00
|
|
|
{
|
2018-06-21 03:41:07 -03:00
|
|
|
uint32_t idcode = (*(uint32_t *)DBGMCU_BASE);
|
|
|
|
int32_t mcuid = idcode & DEVID_MASK;
|
|
|
|
uint16_t revid = ((idcode & REVID_MASK) >> 16);
|
|
|
|
|
2023-04-14 07:54:34 -03:00
|
|
|
uint8_t *endp = &revstr[max - 1];
|
|
|
|
uint8_t *strp = revstr;
|
2018-06-21 03:41:07 -03:00
|
|
|
|
2023-02-15 20:19:32 -04:00
|
|
|
for (const auto &desc : mcu_descriptions) {
|
|
|
|
if (mcuid == desc.mcuid) {
|
2023-04-14 07:54:34 -03:00
|
|
|
// copy the string in:
|
|
|
|
const char *tmp = desc.desc;
|
|
|
|
while (strp < endp && *tmp) {
|
|
|
|
*strp++ = *tmp++;
|
|
|
|
}
|
2018-06-21 03:41:07 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:54:34 -03:00
|
|
|
// comma-separated:
|
2018-06-21 03:41:07 -03:00
|
|
|
if (strp < endp) {
|
|
|
|
*strp++ = ',';
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:54:34 -03:00
|
|
|
for (const auto &rev : silicon_revs) {
|
|
|
|
if (rev.revid == revid) {
|
|
|
|
if (strp < endp) {
|
|
|
|
*strp++ = rev.rev;
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 03:41:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return strp - revstr;
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
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, ...)
|
|
|
|
{
|
2019-02-08 03:55:27 -04:00
|
|
|
#ifdef BOOTLOADER_DEBUG
|
2018-03-28 21:45:13 -03:00
|
|
|
va_list ap;
|
2019-02-08 01:37:03 -04:00
|
|
|
static bool initialised;
|
2019-05-26 22:38:46 -03:00
|
|
|
static SerialConfig debug_sercfg;
|
2019-02-08 01:37:03 -04:00
|
|
|
char umsg[200];
|
|
|
|
if (!initialised) {
|
|
|
|
initialised = true;
|
2019-05-26 22:38:46 -03:00
|
|
|
debug_sercfg.speed = 57600;
|
|
|
|
sdStart(&BOOTLOADER_DEBUG, &debug_sercfg);
|
2019-02-08 01:37:03 -04:00
|
|
|
}
|
2018-03-28 21:45:13 -03:00
|
|
|
va_start(ap, fmt);
|
2019-02-08 01:37:03 -04:00
|
|
|
uint32_t n = vsnprintf(umsg, sizeof(umsg), fmt, ap);
|
2018-03-28 21:45:13 -03:00
|
|
|
va_end(ap);
|
2019-10-19 17:29:26 -03:00
|
|
|
if (n > sizeof(umsg)) {
|
|
|
|
n = sizeof(umsg);
|
|
|
|
}
|
|
|
|
chnWriteTimeout(&BOOTLOADER_DEBUG, (const uint8_t *)umsg, n, chTimeMS2I(100));
|
2018-07-08 18:39:55 -03:00
|
|
|
#endif
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
|
2023-07-31 03:26:43 -03:00
|
|
|
static void thread_sleep_ms(uint32_t ms)
|
|
|
|
{
|
|
|
|
while (ms > 0) {
|
|
|
|
// don't sleep more than 65 at a time, to cope with 16 bit
|
|
|
|
// timer
|
|
|
|
const uint32_t dt = ms > 65? 65: ms;
|
|
|
|
chThdSleepMilliseconds(dt);
|
|
|
|
ms -= dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 21:45:13 -03:00
|
|
|
// 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<npulses; i++) {
|
|
|
|
led_on(LED_BOOTLOADER);
|
2023-07-31 03:26:43 -03:00
|
|
|
thread_sleep_ms(200);
|
2018-03-28 21:45:13 -03:00
|
|
|
led_off(LED_BOOTLOADER);
|
2023-07-31 03:26:43 -03:00
|
|
|
thread_sleep_ms(200);
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
2023-07-31 03:26:43 -03:00
|
|
|
thread_sleep_ms(2000);
|
2018-03-28 21:45:13 -03:00
|
|
|
}
|
|
|
|
}
|
2018-06-18 18:13:10 -03:00
|
|
|
|
|
|
|
//simple variant of std c function to reduce used flash space
|
|
|
|
void *memcpy(void *dest, const void *src, size_t n)
|
|
|
|
{
|
|
|
|
uint8_t *tdest = (uint8_t *)dest;
|
|
|
|
uint8_t *tsrc = (uint8_t *)src;
|
2018-06-21 03:41:07 -03:00
|
|
|
for (int i=0; i<n; i++) {
|
2018-06-18 18:13:10 -03:00
|
|
|
tdest[i] = tsrc[i];
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2018-06-25 00:39:22 -03:00
|
|
|
//simple variant of std c function to reduce used flash space
|
|
|
|
int strncmp(const char *s1, const char *s2, size_t n)
|
|
|
|
{
|
|
|
|
while ((*s1 != 0) && (*s1 == *s2) && n--) {
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
2018-06-27 22:04:59 -03:00
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-25 00:39:22 -03:00
|
|
|
return (*s1 - *s2);
|
|
|
|
}
|
|
|
|
|
2018-06-18 18:13:10 -03:00
|
|
|
//simple variant of std c function to reduce used flash space
|
|
|
|
int strcmp(const char *s1, const char *s2)
|
|
|
|
{
|
2018-06-21 03:41:07 -03:00
|
|
|
while ((*s1 != 0) && (*s1 == *s2)) {
|
2018-06-18 18:13:10 -03:00
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
return (*s1 - *s2);
|
|
|
|
}
|
2018-06-24 21:28:40 -03:00
|
|
|
|
2018-06-25 00:39:22 -03:00
|
|
|
//simple variant of std c function to reduce used flash space
|
|
|
|
size_t strlen(const char *s1)
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
while (*s1++) ret++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//simple variant of std c function to reduce used flash space
|
|
|
|
void *memset(void *s, int c, size_t n)
|
|
|
|
{
|
|
|
|
uint8_t *b = (uint8_t *)s;
|
|
|
|
while (n--) {
|
|
|
|
*b++ = c;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-26 22:38:46 -03:00
|
|
|
#if defined(BOOTLOADER_DEV_LIST)
|
2018-06-24 21:28:40 -03:00
|
|
|
void lock_bl_port(void)
|
|
|
|
{
|
2018-06-25 00:39:22 -03:00
|
|
|
locked_uart = last_uart;
|
2018-06-24 21:28:40 -03:00
|
|
|
}
|
2018-06-25 00:39:22 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
initialise serial ports
|
|
|
|
*/
|
|
|
|
void init_uarts(void)
|
|
|
|
{
|
2018-07-08 18:39:55 -03:00
|
|
|
#if HAL_USE_SERIAL_USB == TRUE
|
2018-06-25 00:39:22 -03:00
|
|
|
sduObjectInit(&SDU1);
|
2019-04-10 00:46:18 -03:00
|
|
|
sduStart(&SDU1, &serusbcfg1);
|
2018-06-25 00:39:22 -03:00
|
|
|
|
2019-04-10 00:46:18 -03:00
|
|
|
usbDisconnectBus(serusbcfg1.usbp);
|
2023-07-31 03:26:43 -03:00
|
|
|
thread_sleep_ms(1000);
|
2019-04-10 00:46:18 -03:00
|
|
|
usbStart(serusbcfg1.usbp, &usbcfg);
|
|
|
|
usbConnectBus(serusbcfg1.usbp);
|
2018-06-25 00:39:22 -03:00
|
|
|
#endif
|
|
|
|
|
2018-06-25 03:08:51 -03:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-06-25 00:39:22 -03:00
|
|
|
sercfg.speed = BOOTLOADER_BAUDRATE;
|
2023-02-15 20:19:32 -04:00
|
|
|
|
|
|
|
for (const auto &uart : uarts) {
|
2018-07-08 18:39:55 -03:00
|
|
|
#if HAL_USE_SERIAL_USB == TRUE
|
2023-02-15 20:19:32 -04:00
|
|
|
if (uart == (BaseChannel *)&SDU1) {
|
2018-06-25 00:39:22 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2023-02-15 20:19:32 -04:00
|
|
|
sdStart((SerialDriver *)uart, &sercfg);
|
2018-06-25 00:39:22 -03:00
|
|
|
}
|
2018-06-25 03:08:51 -03:00
|
|
|
#endif
|
2018-06-25 00:39:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
set baudrate on the current port
|
|
|
|
*/
|
|
|
|
void port_setbaud(uint32_t baudrate)
|
|
|
|
{
|
2018-07-08 18:39:55 -03:00
|
|
|
#if HAL_USE_SERIAL_USB == TRUE
|
2018-06-25 00:39:22 -03:00
|
|
|
if (uarts[last_uart] == (BaseChannel *)&SDU1) {
|
|
|
|
// can't set baudrate on USB
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-25 03:08:51 -03:00
|
|
|
#if HAL_USE_SERIAL == TRUE
|
2018-06-25 01:10:21 -03:00
|
|
|
memset(&sercfg, 0, sizeof(sercfg));
|
|
|
|
sercfg.speed = baudrate;
|
2018-06-25 00:39:22 -03:00
|
|
|
sdStart((SerialDriver *)uarts[last_uart], &sercfg);
|
2018-06-25 03:08:51 -03:00
|
|
|
#endif
|
2018-06-25 00:39:22 -03:00
|
|
|
}
|
2019-05-26 22:38:46 -03:00
|
|
|
#endif // BOOTLOADER_DEV_LIST
|
2023-07-31 01:15:59 -03:00
|
|
|
|