Function bodies 1,964 total
Boot_Print function · c · L46-L55 (10 LOC)Core/Src/boot_cmd.c
void Boot_Print(const char *fmt, ...)
{
char buf[128];
va_list ap;
va_start(ap, fmt);
int len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (len > 0)
HAL_UART_Transmit(&huart1, (uint8_t *)buf, (uint16_t)len, 100);
}Boot_Cmd_Init function · c · L58-L61 (4 LOC)Core/Src/boot_cmd.c
void Boot_Cmd_Init(void)
{
HAL_UART_Receive_IT(&huart1, &boot_cmd_rxByte, 1);
}Boot_Cmd_RxCallback function · c · L64-L72 (9 LOC)Core/Src/boot_cmd.c
void Boot_Cmd_RxCallback(uint8_t data)
{
uint8_t next = (s_rxHead + 1) % RX_QUEUE_SIZE;
if (next != s_rxTail) {
s_rxQueue[s_rxHead] = data;
s_rxHead = next;
}
HAL_UART_Receive_IT(&huart1, &boot_cmd_rxByte, 1);
}Boot_Cmd_Process function · c · L75-L145 (71 LOC)Core/Src/boot_cmd.c
void Boot_Cmd_Process(void)
{
while (s_rxHead != s_rxTail) {
uint8_t ch = s_rxQueue[s_rxTail];
s_rxTail = (s_rxTail + 1) % RX_QUEUE_SIZE;
if (ch == '\r' || ch == '\n') {
if (s_cmdIdx == 0)
continue;
s_cmdBuf[s_cmdIdx] = '\0';
Boot_Print("\r\n");
/* ---- Parse command ---- */
char c0 = s_cmdBuf[0];
if (c0 == 'L' || c0 == 'l') {
cmd_list();
}
else if ((c0 == 'M' || c0 == 'm') && s_cmdIdx >= 2) {
uint8_t idx = (uint8_t)(s_cmdBuf[1] - '0');
cmd_main_update(idx);
}
else if ((c0 == 'H' || c0 == 'h') && s_cmdIdx >= 2 && s_cmdBuf[1] != '\0'
&& s_cmdBuf[1] != 'E' && s_cmdBuf[1] != 'e') {
/* Hx = handpiece update (but not "HELP" or "H" alone) */
uint8_t idx = (uint8_t)(s_cmdBuf[1] - '0');
cmd_hp_update(idx);
}
else if ((c0 == 'V' || c0 == 'v') && s_cmdIdx == 1) {
cmd_version();
}
else if ((c0 == 'H' || c0 == 'h') && s_cmd_list function · c · L150-L182 (33 LOC)Core/Src/boot_cmd.c
static void cmd_list(void)
{
if (!Boot_USB_IsReady()) {
Boot_Print("ERROR: USB not connected.\r\n");
return;
}
int n = Boot_USB_ScanElfFiles(&s_fileList);
if (n < 0) {
Boot_Print("ERROR: Cannot read USB.\r\n");
s_fileListValid = 0;
return;
}
if (n == 0) {
Boot_Print("No .elf files found.\r\n");
s_fileListValid = 0;
return;
}
s_fileListValid = 1;
Boot_Print("--- ELF Files on USB ---\r\n");
for (int i = 0; i < n; i++) {
/* FAT date: bit15:9=Year(0=1980), bit8:5=Month, bit4:0=Day */
uint16_t fd = s_fileList.files[i].fdate;
uint16_t year = ((fd >> 9) & 0x7F) + 1980;
uint8_t month = (fd >> 5) & 0x0F;
uint8_t day = fd & 0x1F;
Boot_Print(" [%d] %s: %04d-%02d-%02d\r\n",
i + 1, s_fileList.files[i].name,
year, month, day);
}
Boot_Print("Total: %d file(s)\r\n", n);
}cmd_main_update function · c · L187-L288 (102 LOC)Core/Src/boot_cmd.c
static void cmd_main_update(uint8_t index)
{
if (!s_fileListValid) {
Boot_Print("Run L first to scan files.\r\n");
return;
}
if (index < 1 || index > s_fileList.count) {
Boot_Print("Invalid file index.\r\n");
return;
}
FIL fp;
if (Boot_USB_OpenFile(&s_fileList, index, &fp) != 0) {
Boot_Print("[407] Cannot open file.\r\n");
return;
}
/* Parse ELF */
Elf_Info_t elf;
if (Elf_Parse(&fp, &elf) != 0) {
Boot_Print("[407] Invalid ELF file.\r\n");
f_close(&fp);
return;
}
Boot_Print("[407] ELF: %d segment(s), %luKB\r\n",
elf.seg_count, (unsigned long)(elf.total_flash_size / 1024));
/* Validate address range */
if (elf.flash_min < APP_FLASH_BASE || elf.flash_max > (APP_FLASH_END + 1)) {
Boot_Print("[407] ERROR: Address out of app range!\r\n");
f_close(&fp);
return;
}
/* Record pre-update version */
Boot_Version_RecordPre();
/* Erase */
Boot_Print("[407] Erasing app sectors...\r\n");
if (Bootcmd_hp_update function · c · L293-L330 (38 LOC)Core/Src/boot_cmd.c
static void cmd_hp_update(uint8_t index)
{
if (!s_fileListValid) {
Boot_Print("Run L first to scan files.\r\n");
return;
}
if (index < 1 || index > s_fileList.count) {
Boot_Print("Invalid file index.\r\n");
return;
}
FIL fp;
if (Boot_USB_OpenFile(&s_fileList, index, &fp) != 0) {
Boot_Print("[H562] Cannot open file.\r\n");
return;
}
Elf_Info_t elf;
if (Elf_Parse(&fp, &elf) != 0) {
Boot_Print("[H562] Invalid ELF file.\r\n");
f_close(&fp);
return;
}
Boot_Print("[H562] ELF: %d segment(s), %luKB\r\n",
elf.seg_count, (unsigned long)(elf.total_flash_size / 1024));
int ret = Boot_HP_UpdateFirmware(&fp, &elf);
f_close(&fp);
Boot_USB_Unmount();
if (ret == 0) {
Boot_Print("[H562] Update OK!\r\n");
} else {
Boot_Print("[H562] Update FAILED (err=%d)\r\n", ret);
}
}Open data scored by Repobility · https://repobility.com
cmd_version function · c · L335-L348 (14 LOC)Core/Src/boot_cmd.c
static void cmd_version(void)
{
Boot_Print("--- Version Info ---\r\n");
Boot_Print("Bootloader : %s\r\n", Boot_Version_GetBoot());
Boot_Print("407 App : %s\r\n", Boot_Version_GetApp());
/* Query handpiece version via RS485 */
char hp_ver[16];
if (Boot_HP_GetVersion(hp_ver, sizeof(hp_ver)) == 0) {
Boot_Print("H562 : %s\r\n", hp_ver);
} else {
Boot_Print("H562 : (no response)\r\n");
}
}cmd_help function · c · L353-L357 (5 LOC)Core/Src/boot_cmd.c
static void cmd_help(void)
{
Boot_Print("\r\nL=List Mx=407update Hx=H562update\r\n");
Boot_Print("V=Version T=RS485test B=Boot H=Help\r\n");
}cmd_rs485_test function · c · L362-L407 (46 LOC)Core/Src/boot_cmd.c
static void cmd_rs485_test(void)
{
uint8_t rx_buf[32];
uint16_t rx_cnt = 0;
uint32_t start;
/* Disable UART6 interrupts, use register-level access */
USART6->CR1 &= ~(USART_CR1_RXNEIE | USART_CR1_TXEIE);
/* Clear any pending errors/data */
while (USART6->SR & USART_SR_RXNE)
(void)USART6->DR;
if (USART6->SR & (USART_SR_ORE | USART_SR_FE | USART_SR_NE))
(void)USART6->DR;
/* TX: send @U*55\n + padding via register */
Boot_Print("TX @U*55 ...");
const char cmd[] = "@U*55\n\n\n";
for (int i = 0; cmd[i]; i++) {
while (!(USART6->SR & USART_SR_TXE));
USART6->DR = cmd[i];
}
while (!(USART6->SR & USART_SR_TC)); /* Wait TX complete */
/* RX: poll for response (3 seconds) */
start = HAL_GetTick();
while ((HAL_GetTick() - start) < 3000 && rx_cnt < sizeof(rx_buf)) {
uint32_t sr = USART6->SR;
if (sr & USART_SR_RXNE) {
rx_buf[rx_cnt++] = (uint8_t)(USART6->DR & 0xFF);
} else if (sr & (USART_SR_ORE | USART_SR_FE | USART_SR_NE)) {
flash_progress_cb function · c · L410-L415 (6 LOC)Core/Src/boot_cmd.c
static void flash_progress_cb(const char *phase, uint8_t pct)
{
Boot_Print("\r[407] %s... %d%%", phase, pct);
if (pct >= 100)
Boot_Print("\r\n");
}Elf_Parse function · c · L52-L125 (74 LOC)Core/Src/boot_elf.c
int Elf_Parse(FIL *fp, Elf_Info_t *info)
{
memset(info, 0, sizeof(*info));
/* Read ELF header */
Elf32_Ehdr ehdr;
UINT br;
if (f_lseek(fp, 0) != FR_OK)
return -1;
if (f_read(fp, &ehdr, sizeof(ehdr), &br) != FR_OK || br != sizeof(ehdr))
return -1;
/* Validate magic */
if (ehdr.e_ident[0] != ELFMAG0 || ehdr.e_ident[1] != ELFMAG1 ||
ehdr.e_ident[2] != ELFMAG2 || ehdr.e_ident[3] != ELFMAG3)
return -1;
/* Must be 32-bit, little-endian, ARM */
if (ehdr.e_ident[4] != ELFCLASS32)
return -1;
if (ehdr.e_ident[5] != ELFDATA2LSB)
return -1;
if (ehdr.e_machine != EM_ARM)
return -1;
/* Validate program header info */
if (ehdr.e_phnum == 0 || ehdr.e_phentsize < sizeof(Elf32_Phdr))
return -1;
/* Read program headers and extract PT_LOAD segments */
uint32_t flash_min = 0xFFFFFFFF;
uint32_t flash_max = 0;
uint8_t seg_idx = 0;
for (uint16_t i = 0; i < ehdr.e_phnum && seg_idx < ELF_MAX_SEGMENTS; i++) {
uint32_t offset = ehdElf_ReadAt function · c · L128-L164 (37 LOC)Core/Src/boot_elf.c
int Elf_ReadAt(FIL *fp, const Elf_Info_t *info,
uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
/* Fill with 0xFF (erased flash state) by default */
memset(buf, 0xFF, len);
for (uint8_t s = 0; s < info->seg_count; s++) {
const Elf_Segment_t *seg = &info->seg[s];
/* Calculate overlap between [flash_addr, flash_addr+len)
and [seg->flash_addr, seg->flash_addr+seg->file_size) */
uint32_t seg_start = seg->flash_addr;
uint32_t seg_end = seg->flash_addr + seg->file_size;
uint32_t req_start = flash_addr;
uint32_t req_end = flash_addr + len;
if (req_start >= seg_end || req_end <= seg_start)
continue; /* No overlap */
uint32_t overlap_start = (req_start > seg_start) ? req_start : seg_start;
uint32_t overlap_end = (req_end < seg_end) ? req_end : seg_end;
uint32_t overlap_len = overlap_end - overlap_start;
/* Calculate file position and buffer position */
uint32_t file_pos = seg->file_offset + (overlBoot_Flash_EraseApp function · c · L25-L55 (31 LOC)Core/Src/boot_flash.c
Boot_Flash_Status_t Boot_Flash_EraseApp(Boot_Flash_ProgressCb cb)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR |
FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
FLASH_EraseInitTypeDef erase;
uint32_t sectorError = 0;
for (uint32_t i = 0; i < NUM_APP_SECTORS; i++) {
erase.TypeErase = FLASH_TYPEERASE_SECTORS;
erase.Banks = FLASH_BANK_1;
erase.Sector = s_app_sectors[i].sector_id;
erase.NbSectors = 1;
erase.VoltageRange = FLASH_VOLTAGE_RANGE_3; /* 2.7~3.6V, word program */
if (HAL_FLASHEx_Erase(&erase, §orError) != HAL_OK) {
HAL_FLASH_Lock();
return BOOT_FLASH_ERR_ERASE;
}
if (cb) {
uint8_t pct = (uint8_t)(((i + 1) * 100U) / NUM_APP_SECTORS);
cb("Erase", pct);
}
}
HAL_FLASH_Lock();
return BOOT_FLASH_OK;
}Boot_Flash_Program function · c · L58-L83 (26 LOC)Core/Src/boot_flash.c
Boot_Flash_Status_t Boot_Flash_Program(uint32_t addr, const uint8_t *data, uint32_t len)
{
/* Validate address range */
if (addr < APP_FLASH_BASE || (addr + len) > (APP_FLASH_END + 1))
return BOOT_FLASH_ERR_ADDR;
HAL_FLASH_Unlock();
/* Program word by word (32-bit) */
uint32_t i = 0;
while (i < len) {
uint32_t word = 0xFFFFFFFF;
uint32_t remaining = len - i;
uint32_t copyLen = (remaining >= 4) ? 4 : remaining;
memcpy(&word, &data[i], copyLen);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr + i, word) != HAL_OK) {
HAL_FLASH_Lock();
return BOOT_FLASH_ERR_PROGRAM;
}
i += 4;
}
HAL_FLASH_Lock();
return BOOT_FLASH_OK;
}Repobility — same analyzer, your code, free for public repos · /scan/
Boot_Flash_Verify function · c · L86-L95 (10 LOC)Core/Src/boot_flash.c
Boot_Flash_Status_t Boot_Flash_Verify(uint32_t addr, const uint8_t *data, uint32_t len)
{
const uint8_t *flash = (const uint8_t *)addr;
for (uint32_t i = 0; i < len; i++) {
if (flash[i] != data[i])
return BOOT_FLASH_ERR_VERIFY;
}
return BOOT_FLASH_OK;
}Boot_Flash_IsAppValid function · c · L98-L116 (19 LOC)Core/Src/boot_flash.c
uint8_t Boot_Flash_IsAppValid(void)
{
uint32_t sp = *(volatile uint32_t *)(APP_FLASH_BASE); /* Initial SP */
uint32_t pc = *(volatile uint32_t *)(APP_FLASH_BASE + 4); /* Reset_Handler */
/* SP must point to RAM */
if ((sp < 0x20000000) || (sp > 0x20020000))
return 0;
/* PC must point to flash (app area) */
if ((pc < APP_FLASH_BASE) || (pc > APP_FLASH_END))
return 0;
/* SP must be word-aligned */
if (sp & 0x3)
return 0;
return 1;
}bkp_enable function · c · L120-L126 (7 LOC)Core/Src/boot_flash.c
static void bkp_enable(void)
{
/* Enable PWR clock and backup domain access (register level, no HAL needed) */
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
PWR->CR |= PWR_CR_DBP;
}Boot_Flash_RequestBoot function · c · L129-L134 (6 LOC)Core/Src/boot_flash.c
void Boot_Flash_RequestBoot(void)
{
bkp_enable();
RTC->BKP0R = BOOT_MAGIC_VALUE;
NVIC_SystemReset();
}Boot_Flash_CheckAndJump function · c · L137-L169 (33 LOC)Core/Src/boot_flash.c
uint8_t Boot_Flash_CheckAndJump(void)
{
/* Called at very beginning of main(), before HAL_Init() */
bkp_enable();
if (RTC->BKP0R != BOOT_MAGIC_VALUE)
return 0;
/* Clear magic so next reset goes to bootloader normally */
RTC->BKP0R = 0;
if (!Boot_Flash_IsAppValid())
return 0;
/* ---- Direct jump (hardware is clean after system reset) ---- */
typedef void (*pFunction)(void);
uint32_t appStack = *(volatile uint32_t *)(APP_FLASH_BASE);
uint32_t appEntry = *(volatile uint32_t *)(APP_FLASH_BASE + 4);
/* Set vector table to app */
SCB->VTOR = APP_FLASH_BASE;
/* Set MSP */
__set_MSP(appStack);
/* Jump to app's Reset_Handler */
pFunction jump = (pFunction)appEntry;
jump();
while (1) {}
return 1;
}Boot_Flash_JumpToApp function · c · L172-L175 (4 LOC)Core/Src/boot_flash.c
void Boot_Flash_JumpToApp(void)
{
Boot_Flash_RequestBoot();
}uart6_set_baud function · c · L23-L33 (11 LOC)Core/Src/boot_hp.c
static void uart6_set_baud(uint32_t baud)
{
/* Re-init USART6 via HAL to change BRR cleanly. */
extern UART_HandleTypeDef huart6;
huart6.Init.BaudRate = baud;
(void)HAL_UART_DeInit(&huart6);
(void)HAL_UART_Init(&huart6);
/* Keep using polling mode */
HAL_NVIC_DisableIRQ(USART6_IRQn);
uart6_flush_rx();
}uart6_send function · c · L35-L43 (9 LOC)Core/Src/boot_hp.c
static void uart6_send(const uint8_t *data, uint16_t len)
{
for (uint16_t i = 0; i < len; i++) {
while (!(USART6->SR & USART_SR_TXE));
USART6->DR = data[i];
}
while (!(USART6->SR & USART_SR_TC));
}About: code-quality intelligence by Repobility · https://repobility.com
uart6_recv_byte function · c · L44-L60 (17 LOC)Core/Src/boot_hp.c
static int uart6_recv_byte(uint8_t *byte, uint32_t timeout_ms)
{
uint32_t start = HAL_GetTick();
while ((HAL_GetTick() - start) < timeout_ms) {
uint32_t sr = USART6->SR;
/* Check RX first (before error clear which also reads DR) */
if (sr & USART_SR_RXNE) {
*byte = (uint8_t)(USART6->DR & 0xFF);
return 1;
}
/* Clear errors only when no data */
if (sr & (USART_SR_ORE | USART_SR_FE | USART_SR_NE))
(void)USART6->DR;
}
return 0; /* Timeout */
}uart6_flush_rx function · c · L61-L68 (8 LOC)Core/Src/boot_hp.c
static void uart6_flush_rx(void)
{
while (USART6->SR & USART_SR_RXNE)
(void)USART6->DR;
if (USART6->SR & (USART_SR_ORE | USART_SR_FE | USART_SR_NE))
(void)USART6->DR;
}uart6_sniff_hex function · c · L71-L90 (20 LOC)Core/Src/boot_hp.c
static void uart6_sniff_hex(uint32_t window_ms, uint16_t max_bytes)
{
uint16_t n = 0;
uint32_t start = HAL_GetTick();
uint32_t last = start;
while ((HAL_GetTick() - start) < window_ms && n < max_bytes) {
uint8_t b;
if (uart6_recv_byte(&b, 2)) {
if (n == 0) Boot_Print("HP RX:");
Boot_Print(" %02X", b);
n++;
last = HAL_GetTick();
continue;
}
if (n > 0 && (HAL_GetTick() - last) > 20)
break;
}
if (n > 0) Boot_Print("\r\n");
}Boot_HP_Init function · c · L94-L99 (6 LOC)Core/Src/boot_hp.c
void Boot_HP_Init(void)
{
/* Use register polling only; keep NVIC/USART6 IRQ from interfering */
HAL_NVIC_DisableIRQ(USART6_IRQn);
uart6_flush_rx();
}hp_xor_parity function · c · L104-L111 (8 LOC)Core/Src/boot_hp.c
static uint8_t hp_xor_parity(const uint8_t *data, uint8_t len)
{
uint8_t x = 0;
for (uint8_t i = 0; i < len; i++)
x ^= data[i];
return x;
}hp_ascii_send function · c · L112-L121 (10 LOC)Core/Src/boot_hp.c
static void hp_ascii_send(char cmd)
{
uint8_t payload = (uint8_t)cmd;
uint8_t parity = hp_xor_parity(&payload, 1);
char frame[12];
int len = snprintf(frame, sizeof(frame), "@%c*%02X\n\n\n", cmd, parity);
uart6_send((uint8_t *)frame, (uint16_t)len);
HAL_Delay(2); /* Ensure auto-direction transceiver settles */
}hp_ascii_wait_ack function · c · L122-L151 (30 LOC)Core/Src/boot_hp.c
static int hp_ascii_wait_ack(char expected_cmd, uint32_t timeout_ms)
{
uint8_t buf[32];
uint16_t idx = 0;
uint32_t start = HAL_GetTick();
while ((HAL_GetTick() - start) < timeout_ms) {
uint8_t ch;
if (!uart6_recv_byte(&ch, 1))
continue;
/* Some HP bootloaders ACK by echoing the command byte only (e.g., 'U'). */
if (ch == (uint8_t)expected_cmd)
return 0;
if (ch == '@')
idx = 0;
if (idx < sizeof(buf))
buf[idx++] = ch;
if (ch == '\n' && idx >= 6 && buf[0] == '@') {
if (buf[1] == (uint8_t)expected_cmd)
return 0; /* ACK received */
idx = 0;
}
}
return -1; /* Timeout */
}hp_cnnx_xor_parity function · c · L156-L163 (8 LOC)Core/Src/boot_hp.c
static uint8_t hp_cnnx_xor_parity(const uint8_t *data, uint16_t len)
{
uint8_t x = 0;
for (uint16_t i = 0; i < len; i++)
x ^= data[i];
return x;
}If a scraper extracted this row, it came from Repobility (https://repobility.com)
hp_cnnx_sum16 function · c · L171-L178 (8 LOC)Core/Src/boot_hp.c
static uint16_t hp_cnnx_sum16(const uint8_t *data, uint16_t len)
{
uint32_t s = 0;
for (uint16_t i = 0; i < len; i++)
s += data[i];
return (uint16_t)s;
}hp_cnnx_set_check function · c · L179-L212 (34 LOC)Core/Src/boot_hp.c
static void hp_cnnx_set_check(uint8_t *pkt, uint16_t data_end_idx, hp_cnnx_check_mode_t mode)
{
/* data_end_idx: index right after payload (i.e. where check starts) */
uint8_t c0 = 0, c1 = 0;
switch (mode) {
case HP_CNNX_CHECK_ZERO:
c0 = 0x00; c1 = 0x00;
break;
case HP_CNNX_CHECK_XOR_DST_TO_PAYLOAD_DUP: {
uint8_t p = hp_cnnx_xor_parity(&pkt[2], (uint16_t)(data_end_idx - 2));
c0 = p; c1 = p;
break;
}
case HP_CNNX_CHECK_XOR_ALL_EXCL_CHECK_DUP: {
uint8_t p = hp_cnnx_xor_parity(&pkt[0], (uint16_t)data_end_idx);
c0 = p; c1 = p;
break;
}
case HP_CNNX_CHECK_SUM16_DST_TO_PAYLOAD: {
uint16_t s = hp_cnnx_sum16(&pkt[2], (uint16_t)(data_end_idx - 2));
c0 = (uint8_t)(s >> 8);
c1 = (uint8_t)(s & 0xFF);
break;
}
}
pkt[data_end_idx + 0] = c0;
pkt[data_end_idx + 1] = c1;
}hp_cnnx_build function · c · L213-L250 (38 LOC)Core/Src/boot_hp.c
static void hp_cnnx_build(uint8_t *pkt,
uint8_t hdr0,
uint8_t hdr1,
uint8_t dst,
uint8_t src,
uint16_t pid,
uint8_t pid_little_endian,
uint8_t cmd,
const uint8_t *data,
uint8_t ndata,
hp_cnnx_check_mode_t check_mode,
uint8_t trl0,
uint8_t trl1)
{
uint16_t idx = 0;
pkt[idx++] = hdr0;
pkt[idx++] = hdr1;
pkt[idx++] = dst;
pkt[idx++] = src;
if (pid_little_endian) {
pkt[idx++] = (uint8_t)(pid & 0xFF);
pkt[idx++] = (uint8_t)(pid >> 8);
} else {
pkt[idx++] = (uint8_t)(pid >> 8);
pkt[idx++] = (uint8_t)(pid & 0xFF);
}
pkt[idx++] = cmd;
pkt[idx++] = ndata;
if (data && ndata > 0) {
memcpy(&pkt[idx], data, ndata);
idx += ndata;
}
hp_cnnx_sethp_cnnx_wait_cmd function · c · L251-L295 (45 LOC)Core/Src/boot_hp.c
static int hp_cnnx_wait_cmd(uint8_t expected_cmd, uint32_t timeout_ms)
{
uint8_t buf[CNNX_PKT_OVERHEAD + CNNX_MAX_DATA];
uint16_t idx = 0;
int state = 0;
int ndata = 0;
int expected_len = 0;
uint32_t start = HAL_GetTick();
while ((HAL_GetTick() - start) < timeout_ms) {
uint8_t ch;
if (!uart6_recv_byte(&ch, 1))
continue;
switch (state) {
case 0:
if (ch == CNNX_HDR_0) { buf[0] = ch; idx = 1; state = 1; }
break;
case 1:
if (ch == CNNX_HDR_1) { buf[1] = ch; idx = 2; state = 2; }
else state = 0;
break;
case 2:
buf[idx++] = ch;
if (idx == 8) { /* After NDATA byte */
ndata = ch;
expected_len = ndata + CNNX_PKT_OVERHEAD;
}
if (idx >= CNNX_PKT_OVERHEAD && idx >= expected_len) {
if (buf[idx - 2] == CNNX_TRL_0 && buf[idx - 1] == CNNX_TRL_1) {
uint8_t cmd = buf[6];
if (cmd == expected_cmd)
return 0;
if (cmd == CNNX_CMD_FWFAILURE || Boot_HP_BootApp function · c · L300-L306 (7 LOC)Core/Src/boot_hp.c
void Boot_HP_BootApp(void)
{
uart6_flush_rx();
hp_ascii_send('A');
hp_ascii_wait_ack('A', HP_ASCII_TIMEOUT_MS);
}Boot_HP_GetVersion function · c · L309-L336 (28 LOC)Core/Src/boot_hp.c
int Boot_HP_GetVersion(char *ver_buf, uint8_t buf_size)
{
uart6_flush_rx();
hp_ascii_send('U');
/* Wait for response - HP might echo 'U' as ACK */
uint8_t buf[32];
uint16_t idx = 0;
uint32_t start = HAL_GetTick();
while ((HAL_GetTick() - start) < 2000) {
uint8_t ch;
if (!uart6_recv_byte(&ch, 1))
continue;
if (ch == '@') idx = 0;
if (idx < sizeof(buf)) buf[idx++] = ch;
if (ch == '\n' && idx >= 6 && buf[0] == '@' && buf[1] == 'V') {
uint8_t vi = 0;
for (uint16_t i = 2; i < idx && buf[i] != '*'; i++) {
if (vi < buf_size - 1)
ver_buf[vi++] = (char)buf[i];
}
ver_buf[vi] = '\0';
return 0;
}
}
return -1;
}Boot_HP_UpdateFirmware function · c · L339-L569 (231 LOC)Core/Src/boot_hp.c
int Boot_HP_UpdateFirmware(FIL *fp, const Elf_Info_t *elf)
{
int ret;
uart6_flush_rx();
s_packetId = 0;
/* ---- Phase 1: ASCII - Enter update mode ---- */
Boot_Print("[H562] Sending update cmd...\r\n");
hp_ascii_send('U');
ret = hp_ascii_wait_ack('U', HP_ASCII_TIMEOUT_MS);
if (ret != 0) {
Boot_Print("[H562] No response.\r\n");
return -1;
}
Boot_Print("[H562] ACK received.\r\n");
HAL_Delay(200);
uart6_flush_rx();
/* ---- Phase 2: CNNX - Firmware transfer ---- */
uint32_t fw_size = elf->total_flash_size;
uint32_t block_cnt = (fw_size + CNNX_MAX_DATA - 1) / CNNX_MAX_DATA;
uint8_t prep_data[10];
prep_data[0] = (uint8_t)(fw_size >> 24);
prep_data[1] = (uint8_t)(fw_size >> 16);
prep_data[2] = (uint8_t)(fw_size >> 8);
prep_data[3] = (uint8_t)(fw_size);
prep_data[4] = (uint8_t)(block_cnt >> 24);
prep_data[5] = (uint8_t)(block_cnt >> 16);
prep_data[6] = (uint8_t)(block_cnt >> 8);
prep_data[7] = (uint8_t)(block_cnt);
prep_data[8Boot_USB_IsReady function · c · L19-L22 (4 LOC)Core/Src/boot_usb.c
uint8_t Boot_USB_IsReady(void)
{
return (Appli_state == APPLICATION_READY) ? 1 : 0;
}Open data scored by Repobility · https://repobility.com
Boot_USB_Mount function · c · L25-L36 (12 LOC)Core/Src/boot_usb.c
int Boot_USB_Mount(void)
{
if (s_mounted)
return 0;
FRESULT res = f_mount(&USBHFatFS, (TCHAR const *)USBHPath, 1);
if (res != FR_OK)
return -1;
s_mounted = 1;
return 0;
}Boot_USB_Unmount function · c · L39-L45 (7 LOC)Core/Src/boot_usb.c
void Boot_USB_Unmount(void)
{
if (s_mounted) {
f_mount(NULL, (TCHAR const *)USBHPath, 0);
s_mounted = 0;
}
}str_endswith_elf function · c · L48-L57 (10 LOC)Core/Src/boot_usb.c
static int str_endswith_elf(const char *name)
{
size_t len = strlen(name);
if (len < 5) return 0;
const char *ext = &name[len - 4];
return (ext[0] == '.' &&
(ext[1] == 'E' || ext[1] == 'e') &&
(ext[2] == 'L' || ext[2] == 'l') &&
(ext[3] == 'F' || ext[3] == 'f'));
}Boot_USB_ScanElfFiles function · c · L60-L97 (38 LOC)Core/Src/boot_usb.c
int Boot_USB_ScanElfFiles(USB_FileList_t *list)
{
memset(list, 0, sizeof(*list));
if (Boot_USB_Mount() != 0)
return -1;
DIR dir;
FILINFO fno;
if (f_opendir(&dir, "/") != FR_OK) {
Boot_USB_Unmount();
return -1;
}
while (list->count < USB_MAX_FILES) {
if (f_readdir(&dir, &fno) != FR_OK || fno.fname[0] == 0)
break;
/* Skip directories */
if (fno.fattrib & AM_DIR)
continue;
/* Filter .elf files only */
if (!str_endswith_elf(fno.fname))
continue;
strncpy(list->files[list->count].name, fno.fname, 12);
list->files[list->count].name[12] = '\0';
list->files[list->count].size = (uint32_t)fno.fsize;
list->files[list->count].fdate = fno.fdate;
list->count++;
}
f_closedir(&dir);
/* Keep mounted for file operations */
return (int)list->count;
}Boot_USB_OpenFile function · c · L100-L113 (14 LOC)Core/Src/boot_usb.c
int Boot_USB_OpenFile(const USB_FileList_t *list, uint8_t index, FIL *fp)
{
if (index < 1 || index > list->count)
return -1;
if (Boot_USB_Mount() != 0)
return -1;
const char *name = list->files[index - 1].name;
if (f_open(fp, name, FA_READ) != FR_OK)
return -1;
return 0;
}date_to_version function · c · L48-L72 (25 LOC)Core/Src/boot_version.c
static void date_to_version(const char *date_str, char *out)
{
/* __DATE__ format: "Mmm DD YYYY" */
int month = 0;
for (int i = 0; i < 12; i++) {
if (date_str[0] == s_months[i][0] &&
date_str[1] == s_months[i][1] &&
date_str[2] == s_months[i][2]) {
month = i + 1;
break;
}
}
int day = 0;
if (date_str[4] == ' ')
day = date_str[5] - '0';
else
day = (date_str[4] - '0') * 10 + (date_str[5] - '0');
/* Year is at offset 7 */
if (month < 1 || month > 12) month = 0;
if (day < 1 || day > 31) day = 0;
snprintf(out, VERSION_STR_LEN, "%.4s-%02u-%02u",
&date_str[7], (unsigned)month, (unsigned)day);
}Boot_Version_GetBoot function · c · L75-L80 (6 LOC)Core/Src/boot_version.c
const char* Boot_Version_GetBoot(void)
{
if (s_bootVer[0] == '\0')
date_to_version(__DATE__, s_bootVer);
return s_bootVer;
}Boot_Version_GetApp function · c · L83-L94 (12 LOC)Core/Src/boot_version.c
const char* Boot_Version_GetApp(void)
{
const VerStore_t *store = (const VerStore_t *)VER_STORE_ADDR;
if (store->magic == VER_MAGIC && store->post_version[0] != '\0' &&
store->post_version[0] != (char)0xFF) {
strncpy(s_appVer, store->post_version, VERSION_STR_LEN - 1);
s_appVer[VERSION_STR_LEN - 1] = '\0';
return s_appVer;
}
return "unknown";
}Repobility — same analyzer, your code, free for public repos · /scan/
Boot_Version_RecordPre function · c · L97-L102 (6 LOC)Core/Src/boot_version.c
void Boot_Version_RecordPre(void)
{
/* Read current version before update */
const char *cur = Boot_Version_GetApp();
Boot_Print("[VER] Pre-update: %s\r\n", cur);
}Boot_Version_RecordPost function · c · L105-L160 (56 LOC)Core/Src/boot_version.c
void Boot_Version_RecordPost(const char *filename)
{
/* Try to extract version from filename (e.g., "RF_NE~1.ELF" or similar)
For 8.3 filenames, just record the filename itself.
The actual version is derived from the build date of the new firmware,
which we can read from the ELF. For simplicity, we use current date. */
VerStore_t newStore;
newStore.magic = VER_MAGIC;
/* Copy pre-update version */
const VerStore_t *oldStore = (const VerStore_t *)VER_STORE_ADDR;
if (oldStore->magic == VER_MAGIC) {
strncpy(newStore.pre_version, oldStore->post_version, 11);
} else {
strncpy(newStore.pre_version, "none", 11);
}
newStore.pre_version[11] = '\0';
/* Post-update version = current date (build date of bootloader as proxy) */
date_to_version(__DATE__, newStore.post_version);
/* Record filename */
strncpy(newStore.filename, filename ? filename : "unknown", 15);
newStore.filename[15] = '\0';
/* Write to flash (word by word) - this area is in MX_GPIO_Init function · c · L42-L64 (23 LOC)Core/Src/gpio.c
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET);
/*Configure GPIO pins : PE2 PE3 */
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}page 1 / 40next ›