mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-19 19:40:56 +00:00
Add MG_OTA_{ROLLBACK,STATE_SET,STATE_GET}
This commit is contained in:
1 parent
a6452f5311
commit
67f249c58a
16 files changed
+345
-131
No files matched your search
@@ -27,7 +27,7 @@ firmware.bin: firmware.elf
|
||||
@echo "To flash, run 'make flash', or use STM32CubeProgrammer"
|
||||
|
||||
flash: firmware.bin
|
||||
STM32_Programmer_CLI -c port=swd -e all -w firmware.elf -hardRst
|
||||
STM32_Programmer_CLI -c port=swd -w firmware.elf -hardRst
|
||||
|
||||
cmsis_core:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@
|
||||
|
||||
@@ -51,9 +51,6 @@ enum { PLL1_HSI = 64, PLL1_M = 32, PLL1_N = 225, PLL1_P = 1 };
|
||||
#define APB2_FREQUENCY (AHB_FREQUENCY / (BIT(HAL_D2PPRE2 - 3)))
|
||||
#define APB1_FREQUENCY (AHB_FREQUENCY / (BIT(HAL_D2PPRE1 - 3)))
|
||||
|
||||
static inline void hal_spin(volatile uint32_t n) {
|
||||
while (n--) (void) 0;
|
||||
}
|
||||
|
||||
enum { HAL_GPIO_MODE_INPUT, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_MODE_AF, HAL_GPIO_MODE_ANALOG };
|
||||
enum { HAL_GPIO_OTYPE_PUSH_PULL, HAL_GPIO_OTYPE_OPEN_DRAIN };
|
||||
@@ -124,7 +121,7 @@ static inline void hal_uart_init(USART_TypeDef *uart, uint16_t tx,
|
||||
}
|
||||
static inline void hal_uart_write_byte(USART_TypeDef *uart, uint8_t byte) {
|
||||
uart->TDR = byte;
|
||||
while ((uart->ISR & BIT(7)) == 0) hal_spin(1);
|
||||
while ((uart->ISR & USART_ISR_TXE_TXFNF) == 0) (void) 0;
|
||||
}
|
||||
static inline void hal_uart_write_buf(USART_TypeDef *uart, char *buf, size_t len) {
|
||||
while (len-- > 0) hal_uart_write_byte(uart, *(uint8_t *) buf++);
|
||||
@@ -146,7 +143,7 @@ static inline void hal_rng_init(void) {
|
||||
RNG->HTCR = 0x17590abc;
|
||||
RNG->HTCR = 0xaa74;
|
||||
RNG->CR &= ~RNG_CR_CONDRST ;
|
||||
while(RNG->CR & RNG_CR_CONDRST) hal_spin(1); // 39.7.1
|
||||
while (RNG->CR & RNG_CR_CONDRST) (void) 0; // 39.7.1
|
||||
RNG->CR |= RNG_CR_RNGEN; // Enable RNG
|
||||
}
|
||||
|
||||
@@ -184,11 +181,19 @@ static inline unsigned int hal_pllrge(unsigned int f) {
|
||||
return val - 1;
|
||||
}
|
||||
|
||||
// Enable RTC/BKP APB clock and backup domain write access.
|
||||
// Must be called before accessing RTC backup registers or IWDG.
|
||||
static inline void hal_backup_domain_init(void) {
|
||||
RCC->APB4ENR |= RCC_APB4ENR_RTCAPBEN; // enable RTC/BKP APB clock
|
||||
PWR->CR1 |= PWR_CR1_DBP; // enable backup domain write access
|
||||
}
|
||||
|
||||
static inline void hal_clock_init(void) {
|
||||
PWR->CR3 |= BIT(1); // select LDO (reset value)
|
||||
while ((PWR->CSR1 & BIT(13)) == 0) hal_spin(1); // ACTVOSRDY
|
||||
PWR->D3CR &= ~(BIT(15) | BIT(14)); // Select VOS0
|
||||
while ((PWR->D3CR & BIT(13)) == 0) hal_spin(1); // VOSRDY
|
||||
hal_backup_domain_init();
|
||||
PWR->CR3 |= PWR_CR3_LDOEN; // select LDO (reset value)
|
||||
while ((PWR->CSR1 & PWR_CSR1_ACTVOSRDY) == 0) (void) 0; // ACTVOSRDY
|
||||
PWR->D3CR &= ~PWR_D3CR_VOS_Msk; // Select VOS0
|
||||
while ((PWR->D3CR & PWR_D3CR_VOSRDY) == 0) (void) 0; // VOSRDY
|
||||
CLRSET(
|
||||
RCC->D1CFGR, (0x0F << 8) | (7 << 4) | (0x0F << 0),
|
||||
(hal_div2prescval(HAL_D1CPRE) << 8) | (HAL_D1PPRE << 4) | (hal_div2prescval(HAL_HPRE) << 0));
|
||||
@@ -201,17 +206,18 @@ static inline void hal_clock_init(void) {
|
||||
((PLL1_P - 1) << 9) | ((PLL1_N - 1) << 0)); // Set PLL1_P PLL1_N
|
||||
CLRSET(RCC->PLLCKSELR, 0x3F << 4,
|
||||
PLL1_M << 4); // Set PLL1_M (source defaults to HSI)
|
||||
RCC->CR |= BIT(24) | BIT(12); // Enable PLL1 and HSI48 (for RNG)
|
||||
while ((RCC->CR & BIT(25)) == 0) hal_spin(1); // Wait until done
|
||||
RCC->CFGR |= (3 << 0); // Set clock source to PLL1
|
||||
while ((RCC->CFGR & (7 << 3)) != (3 << 3)) hal_spin(1); // Wait until done
|
||||
FLASH->ACR = HAL_FLASH_LATENCY; // default is larger
|
||||
RCC->APB4ENR |= RCC_APB4ENR_SYSCFGEN; // Enable SYSCFG
|
||||
while ((RCC->CR & BIT(13)) == 0) hal_spin(1); // Make sure HSI48 is ready
|
||||
RCC->CR |= RCC_CR_PLL1ON | RCC_CR_HSI48ON; // Enable PLL1 and HSI48 (for RNG)
|
||||
while ((RCC->CR & RCC_CR_PLL1RDY) == 0) (void) 0; // Wait until done
|
||||
RCC->CFGR |= (3 << 0); // Set clock source to PLL1
|
||||
while ((RCC->CFGR & (7 << 3)) != (3 << 3)) (void) 0; // Wait until done
|
||||
FLASH->ACR = HAL_FLASH_LATENCY; // default is larger
|
||||
RCC->APB4ENR |= RCC_APB4ENR_SYSCFGEN; // Enable SYSCFG
|
||||
while ((RCC->CR & RCC_CR_HSI48RDY) == 0) (void) 0; // Make sure HSI48 is ready
|
||||
SystemCoreClock = SYS_FREQUENCY; // Update SystemCoreClock global var
|
||||
SysTick_Config(SystemCoreClock / 1000); // Sys tick every 1ms
|
||||
}
|
||||
|
||||
|
||||
static inline void hal_system_init(void) {
|
||||
SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); // Enable FPU
|
||||
__DSB();
|
||||
|
||||
@@ -43,13 +43,30 @@ bool mg_random(void *buf, size_t len) {
|
||||
}
|
||||
|
||||
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) { // New HTTP request received
|
||||
if (ev == MG_EV_HTTP_HDRS) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/api/ota/update"), NULL)) {
|
||||
mg_http_start_ota(c, hm, NULL);
|
||||
}
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) {
|
||||
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), hal_get_tick());
|
||||
} else if (mg_match(hm->uri, mg_str("/api/ota/commit"), NULL)) {
|
||||
c->data[0] = 1;
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else if (mg_match(hm->uri, mg_str("/api/ota/rollback"), NULL)) {
|
||||
c->data[0] = 2;
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else {
|
||||
mg_http_reply(c, 200, "", "Hi from Mongoose, tick %llu\n", hal_get_tick());
|
||||
}
|
||||
} else if (ev == MG_EV_CLOSE && c->data[0] == 1) {
|
||||
MG_OTA_STATE_SET(MG_OTA_CONFIRMED);
|
||||
NVIC_SystemReset();
|
||||
} else if (ev == MG_EV_CLOSE && c->data[0] == 2) {
|
||||
MG_OTA_STATE_SET(MG_OTA_CONFIRMED);
|
||||
MG_OTA_ROLLBACK();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,9 +79,11 @@ int main(void) {
|
||||
hal_gpio_output(LED2);
|
||||
hal_gpio_output(LED3);
|
||||
|
||||
printf("Initialised. CPU clock: %lu MHz\r\n", SystemCoreClock / 1000000);
|
||||
MG_INFO(("Initialised. CPU clock: %lu MHz", SystemCoreClock / 1000000));
|
||||
|
||||
// Max delay 65 seconds
|
||||
MG_OTA_BOOT_CHECK(30);
|
||||
|
||||
// Start a minimal web server
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "hal.h"
|
||||
|
||||
// See https://mongoose.ws/docs/getting-started/build-options/
|
||||
#define MG_ARCH MG_ARCH_ARMGCC
|
||||
@@ -22,8 +23,27 @@
|
||||
// #define MG_TCPIP_GW MG_IPV4(192, 168, 0, 1) // Gateway
|
||||
// #define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0) // Netmask
|
||||
|
||||
// OTA rollback timer via IWDG1. PR=7 → /512 on H723 (RM0468 §54.4.2).
|
||||
// Max timeout: 4095 * 512 / 32000 = 65 s.
|
||||
// RM0468 §54.4 sequence: 0xCCCC first (starts LSI), then 0x5555 to unlock,
|
||||
// write PR+RLR, wait SR=0 (PVU|RVU), then 0xAAAA to reload the counter.
|
||||
#define MG_OTA_ROLLBACK_TIMER_START(seconds) \
|
||||
do { \
|
||||
IWDG1->KR = 0xCCCCU; /* start/LSI */ \
|
||||
IWDG1->KR = 0x5555U; /* unlock */ \
|
||||
IWDG1->PR = 7U; /* /512 */ \
|
||||
IWDG1->RLR = (uint32_t) ((seconds) * 32000 / 512); \
|
||||
while (IWDG1->SR & (IWDG_SR_PVU | IWDG_SR_RVU)) (void) 0; /* wait SR=0 */ \
|
||||
IWDG1->KR = 0xAAAAU; /* reload */ \
|
||||
} while (0)
|
||||
|
||||
// OTA state in RTC backup register 0 (survives all resets, clears on POR).
|
||||
// Requires hal_backup_domain_init() (called from hal_clock_init) to have run.
|
||||
#define MG_OTA_STATE_GET() (RTC->BKP0R)
|
||||
#define MG_OTA_STATE_SET(v) (RTC->BKP0R = (uint32_t) (v))
|
||||
|
||||
// Construct MAC address from the MCU unique ID
|
||||
#define MGUID ((uint32_t *) 0x1ff1e800) // Unique 96-bit chip ID
|
||||
#define MGUID ((uint32_t *) UID_BASE)
|
||||
#define MG_SET_MAC_ADDRESS(mac) \
|
||||
do { \
|
||||
mac[0] = 2; \
|
||||
|
||||
Reference in new issue
Block a user