mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-28 20:58:31 +00:00
stm32 examples revamp
This commit is contained in:
1 parent
d4013e47f9
commit
21b9cdaef7
1577 files changed
+619
-842795
No files matched your search
@@ -0,0 +1,37 @@
|
||||
# Environment setup: https://mongoose.ws/documentation/tutorials/tools/
|
||||
|
||||
CFLAGS = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion
|
||||
CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion
|
||||
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
|
||||
CFLAGS += -I. -Icmsis_core/CMSIS/Core/Include -Icmsis_u5/Include -Imongoose
|
||||
CFLAGS += -mcpu=cortex-m33 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=hard
|
||||
LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map
|
||||
|
||||
SOURCES = main.c hal.c wifi.c mongoose/mongoose.c
|
||||
SOURCES += cmsis_u5/Source/Templates/gcc/startup_stm32u5a5xx.s
|
||||
|
||||
|
||||
all build example: firmware.bin
|
||||
|
||||
mongoose/mongoose.c mongoose/mongoose.h:
|
||||
cp ../../../../mongoose.[ch] mongoose/
|
||||
|
||||
firmware.bin: firmware.elf
|
||||
arm-none-eabi-objcopy -O binary $< $@
|
||||
@echo
|
||||
@echo "To flash, run 'make flash', or use STM32CubeProgrammer"
|
||||
|
||||
firmware.elf: cmsis_core cmsis_u5 $(SOURCES) mongoose/mongoose.h $(wildcard *.h) link.ld Makefile
|
||||
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
flash: firmware.bin
|
||||
STM32_Programmer_CLI -c port=swd -e all -w $< 0x8000000 -hardRst
|
||||
|
||||
cmsis_core:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@
|
||||
|
||||
cmsis_u5:
|
||||
git clone -q -c advice.detachedHead=false --depth 1 -b v1.4.2 https://github.com/STMicroelectronics/cmsis_device_u5 $@
|
||||
|
||||
clean:
|
||||
rm -rf firmware.* cmsis_* mongoose/mongoose.*
|
||||
@@ -0,0 +1,125 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
bool hal_timer_expired(volatile uint64_t *t, uint64_t period, uint64_t now) {
|
||||
uint64_t diff = now - *t; // Wrap-safe elapsed time since last expiry
|
||||
if (period == 0) return false; // Avoid division by zero
|
||||
if (diff < period) return false; // Period has not elapsed yet
|
||||
*t += (diff / period) * period; // Preserve cadence, skip missed periods
|
||||
return true;
|
||||
}
|
||||
|
||||
static volatile uint64_t s_ticks; // Milliseconds since boot
|
||||
void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms
|
||||
s_ticks++;
|
||||
}
|
||||
|
||||
uint64_t hal_get_tick(void) {
|
||||
return s_ticks;
|
||||
};
|
||||
|
||||
uint32_t SystemCoreClock = SYS_FREQUENCY;
|
||||
void SystemInit(void) { // Called automatically by startup code
|
||||
hal_system_init(); // Enable FPU
|
||||
hal_clock_init();
|
||||
SysTick_Config(SystemCoreClock / 1000); // Sys tick every 1ms
|
||||
}
|
||||
|
||||
void ExitRun0Mode(void) {
|
||||
}
|
||||
|
||||
struct stat;
|
||||
__attribute__((weak)) int _fstat(int fd, struct stat *st) {
|
||||
(void) fd, (void) st;
|
||||
return -1;
|
||||
}
|
||||
|
||||
extern unsigned char _end[]; // End of data section, start of heap. See link.ld
|
||||
static unsigned char *s_current_heap_end = _end;
|
||||
|
||||
size_t hal_ram_used(void) {
|
||||
return (size_t) (s_current_heap_end - _end);
|
||||
}
|
||||
|
||||
size_t hal_ram_free(void) {
|
||||
unsigned char endofstack;
|
||||
return (size_t) (&endofstack - s_current_heap_end);
|
||||
}
|
||||
|
||||
void *_sbrk(int incr) {
|
||||
unsigned char *prev_heap;
|
||||
unsigned char *heap_end = (unsigned char *) ((size_t) &heap_end - 256);
|
||||
prev_heap = s_current_heap_end;
|
||||
// Check how much space we got from the heap end to the stack end
|
||||
if (s_current_heap_end + incr > heap_end) return (void *) -1;
|
||||
s_current_heap_end += incr;
|
||||
return prev_heap;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _open(const char *path) {
|
||||
(void) path;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _close(int fd) {
|
||||
(void) fd;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _isatty(int fd) {
|
||||
(void) fd;
|
||||
return 1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _lseek(int fd, int ptr, int dir) {
|
||||
(void) fd, (void) ptr, (void) dir;
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((weak)) void _exit(int status) {
|
||||
(void) status;
|
||||
for (;;) asm volatile("BKPT #0");
|
||||
}
|
||||
|
||||
__attribute__((weak)) void _kill(int pid, int sig) {
|
||||
(void) pid, (void) sig;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _getpid(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _write(int fd, char *ptr, int len) {
|
||||
(void) fd, (void) ptr, (void) len;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _read(int fd, char *ptr, int len) {
|
||||
(void) fd, (void) ptr, (void) len;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _link(const char *a, const char *b) {
|
||||
(void) a, (void) b;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _unlink(const char *a) {
|
||||
(void) a;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _stat(const char *path, struct stat *st) {
|
||||
(void) path, (void) st;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int mkdir(const char *path, int mode) {
|
||||
(void) path, (void) mode;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) void _init(void) {
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// RM0456
|
||||
// https://www.st.com/resource/en/reference_manual/rm0456-stm32u5-series-armbased-32bit-mcus-stmicroelectronics.pdf
|
||||
// https://www.st.com/resource/en/user_manual/um2861-stm32u5-nucleo144-board-mb1549-stmicroelectronics.pdf
|
||||
|
||||
#pragma once
|
||||
|
||||
#define LED_1 PIN('C', 7) // On-board LED pin (green)
|
||||
#define LED_2 PIN('B', 7) // On-board LED pin (blue)
|
||||
#define LED_3 PIN('G', 2) // On-board LED pin (red)
|
||||
#define LED LED_1 // Use green LED for blinking
|
||||
|
||||
#define SPI_RDY PIN('E', 13)
|
||||
#define SPI_CS PIN('D', 14)
|
||||
#define SPI_CHIP_EN PIN('E', 11)
|
||||
|
||||
#ifndef HAL_DEBUG_UART
|
||||
#define HAL_DEBUG_UART USART1
|
||||
#endif
|
||||
#ifndef UART_DEBUG
|
||||
#define UART_DEBUG HAL_DEBUG_UART
|
||||
#endif
|
||||
|
||||
#ifndef WIFISPI
|
||||
#define WIFISPI SPI1
|
||||
#endif
|
||||
|
||||
#include <stm32u5a5xx.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
extern bool hal_timer_expired(volatile uint64_t *t, uint64_t prd, uint64_t now);
|
||||
extern uint64_t hal_get_tick(void);
|
||||
extern size_t hal_ram_free(void);
|
||||
extern size_t hal_ram_used(void);
|
||||
extern uint32_t SystemCoreClock;
|
||||
|
||||
#define BIT(x) (1UL << (x))
|
||||
#define CLRSET(reg, clear, set) ((reg) = ((reg) & ~(clear)) | (set))
|
||||
#define PIN(bank, num) ((((bank) - 'A') << 8) | (num))
|
||||
#define PINNO(pin) (pin & 255)
|
||||
#define PINBANK(pin) (pin >> 8)
|
||||
|
||||
void hal_init(void);
|
||||
size_t hal_ram_free(void);
|
||||
size_t hal_ram_used(void);
|
||||
|
||||
enum {
|
||||
HAL_D1PRE = 1, // actual divisor value
|
||||
HAL_HPRE = 1, // actual divisor value
|
||||
HAL_D2PRE = 1,
|
||||
HAL_D3PRE = 1,
|
||||
};
|
||||
enum { PLL1_M = 1, PLL1_N = 80, PLL1_P = 2, PLL1_Q = 2, PLL1_R = 2 };
|
||||
#define MSIS_FREQUENCY (4 * 1024 * 1024) // 4 Mhz
|
||||
#define SYS_FREQUENCY (MSIS_FREQUENCY * PLL1_N / PLL1_M / PLL1_P / HAL_D1PRE) // 160MHz
|
||||
#define AHB_FREQUENCY (SYS_FREQUENCY / HAL_HPRE)
|
||||
#define APB2_FREQUENCY (AHB_FREQUENCY / HAL_D2PRE)
|
||||
#define APB1_FREQUENCY (AHB_FREQUENCY / HAL_D1PRE)
|
||||
|
||||
static inline void 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 };
|
||||
enum {
|
||||
HAL_GPIO_SPEED_LOW,
|
||||
HAL_GPIO_SPEED_MEDIUM,
|
||||
HAL_GPIO_SPEED_HIGH,
|
||||
HAL_GPIO_SPEED_INSANE
|
||||
};
|
||||
enum { HAL_GPIO_PULL_NONE, HAL_GPIO_PULL_UP, HAL_GPIO_PULL_DOWN };
|
||||
|
||||
#define HAL_GPIO(N) ((GPIO_TypeDef *)(GPIOA_BASE_NS + 0x400 * (N)))
|
||||
|
||||
static GPIO_TypeDef *hal_gpio_bank(uint16_t pin) {
|
||||
return HAL_GPIO(PINBANK(pin));
|
||||
}
|
||||
static inline void hal_gpio_toggle(uint16_t pin) {
|
||||
GPIO_TypeDef *gpio = hal_gpio_bank(pin);
|
||||
uint32_t mask = BIT(PINNO(pin));
|
||||
gpio->BSRR = mask << (gpio->ODR & mask ? 16 : 0);
|
||||
}
|
||||
static inline int hal_gpio_read(uint16_t pin) {
|
||||
return hal_gpio_bank(pin)->IDR & BIT(PINNO(pin)) ? 1 : 0;
|
||||
}
|
||||
static inline void hal_gpio_write(uint16_t pin, bool val) {
|
||||
GPIO_TypeDef *gpio = hal_gpio_bank(pin);
|
||||
gpio->BSRR = BIT(PINNO(pin)) << (val ? 0 : 16);
|
||||
}
|
||||
static inline void hal_gpio_init(uint16_t pin, uint8_t mode, uint8_t type,
|
||||
uint8_t speed, uint8_t pull, uint8_t af) {
|
||||
GPIO_TypeDef *gpio = hal_gpio_bank(pin);
|
||||
uint8_t n = (uint8_t)(PINNO(pin));
|
||||
RCC->AHB2ENR1 |= BIT(PINBANK(pin)); // Enable GPIO clock
|
||||
CLRSET(gpio->OTYPER, 1UL << n, ((uint32_t)type) << n);
|
||||
CLRSET(gpio->OSPEEDR, 3UL << (n * 2), ((uint32_t)speed) << (n * 2));
|
||||
CLRSET(gpio->PUPDR, 3UL << (n * 2), ((uint32_t)pull) << (n * 2));
|
||||
CLRSET(gpio->AFR[n >> 3], 15UL << ((n & 7) * 4),
|
||||
((uint32_t)af) << ((n & 7) * 4));
|
||||
CLRSET(gpio->MODER, 3UL << (n * 2), ((uint32_t)mode) << (n * 2));
|
||||
}
|
||||
static inline void hal_gpio_input(uint16_t pin) {
|
||||
hal_gpio_init(pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0);
|
||||
}
|
||||
static inline void hal_gpio_output(uint16_t pin) {
|
||||
hal_gpio_init(pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0);
|
||||
}
|
||||
|
||||
// D2 Kernel clock (8.7.21) USART1 defaults to pclk2 (APB2), while USART2,3
|
||||
// default to pclk1 (APB1). Even if using other kernel clocks, the APBx clocks
|
||||
// must be enabled for CPU access, as the kernel clock drives the BRR, not the
|
||||
// APB bus interface
|
||||
static inline void hal_uart_init(USART_TypeDef *uart, unsigned long baud) {
|
||||
uint8_t af = 7; // Alternate function
|
||||
uint16_t rx = 0, tx = 0; // pins
|
||||
uint32_t freq =
|
||||
SYS_FREQUENCY; // Bus frequency. UART1 is on APB2, rest on APB1
|
||||
|
||||
if (uart == USART1)
|
||||
freq = APB2_FREQUENCY, RCC->APB2ENR |= BIT(14);
|
||||
if (uart == USART2)
|
||||
freq = APB1_FREQUENCY, RCC->APB1ENR1 |= BIT(17);
|
||||
if (uart == USART3)
|
||||
freq = APB1_FREQUENCY, RCC->APB1ENR1 |= BIT(18);
|
||||
|
||||
if (uart == USART1)
|
||||
tx = PIN('A', 9), rx = PIN('A', 10);
|
||||
if (uart == USART2)
|
||||
tx = PIN('A', 2), rx = PIN('A', 3);
|
||||
if (uart == USART3)
|
||||
tx = PIN('D', 8), rx = PIN('D', 9);
|
||||
|
||||
#if 0 // CONSTANT BAUD RATE FOR DEBUGGING
|
||||
CLRSET(RCC->CCIPR2, 7 << 3, 3 << 3); // use HSI for UART1
|
||||
freq = 64000000;
|
||||
#endif
|
||||
|
||||
hal_gpio_init(tx, HAL_GPIO_MODE_AF, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_HIGH, 0, af);
|
||||
hal_gpio_init(rx, HAL_GPIO_MODE_AF, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_HIGH, 0, af);
|
||||
uart->CR1 = 0; // Disable this UART
|
||||
uart->BRR = freq / baud; // Set baud rate
|
||||
uart->CR1 = BIT(0) | BIT(2) | BIT(3); // Set UE, RE, TE
|
||||
}
|
||||
static inline void hal_uart_write_byte(USART_TypeDef *uart, uint8_t byte) {
|
||||
uart->TDR = byte;
|
||||
while ((uart->ISR & BIT(7)) == 0)
|
||||
spin(1);
|
||||
}
|
||||
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++);
|
||||
}
|
||||
static inline int hal_uart_read_ready(USART_TypeDef *uart) {
|
||||
return uart->ISR & BIT(5); // If RXNE bit is set, data is ready
|
||||
}
|
||||
static inline uint8_t hal_uart_read_byte(USART_TypeDef *uart) {
|
||||
return (uint8_t)(uart->RDR & 255);
|
||||
}
|
||||
|
||||
static inline void hal_rng_init(void) {
|
||||
RCC->CCIPR2 |= RCC_CCIPR2_RNGSEL_0; // RNG clock source pll1_q_ck
|
||||
RCC->AHB2ENR1 |= RCC_AHB2ENR1_RNGEN; // Enable RNG clock
|
||||
RNG->CR = RNG_CR_RNGEN; // Enable RNG
|
||||
}
|
||||
|
||||
static inline uint32_t hal_rng_read(void) {
|
||||
while ((RNG->SR & RNG_SR_DRDY) == 0)
|
||||
(void)0;
|
||||
return RNG->DR;
|
||||
}
|
||||
|
||||
#define MG_HAL_DISABLE_ETHERNET 1
|
||||
|
||||
static inline unsigned int hal_ahb_div2prescval(unsigned int div) {
|
||||
if (div == 1)
|
||||
return 0;
|
||||
unsigned int val = 7;
|
||||
while (div >>= 1)
|
||||
++val;
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline unsigned int hal_apb_div2prescval(unsigned int div) {
|
||||
if (div <= 1)
|
||||
return 0;
|
||||
unsigned int val = 0;
|
||||
while (div >>= 1)
|
||||
++val;
|
||||
return val + 2;
|
||||
}
|
||||
|
||||
static inline void hal_clock_init(void) {
|
||||
FLASH->ACR |= BIT(8); // prefetch
|
||||
RCC->AHB3ENR |= RCC_AHB3ENR_PWREN;
|
||||
PWR->SVMCR |= PWR_SVMCR_IO2SV; // Enable VDDIO2 supply
|
||||
PWR->UCPDR |= PWR_UCPDR_UCPD_DBDIS; // Disable the internal Pull-Up in Dead
|
||||
// Battery pins of UCPD peripheral
|
||||
PWR->CR3 |= PWR_CR3_REGSEL; // enable SMPS
|
||||
while ((PWR->SVMSR & BIT(1)) == 0)
|
||||
spin(1);
|
||||
if ((PWR->SVMSR & PWR_SVMSR_ACTVOS) != (PWR_VOSR_VOS_0 | PWR_VOSR_VOS_1)) {
|
||||
// control voltage scaling
|
||||
PWR->VOSR |= PWR_VOSR_VOS_0 | PWR_VOSR_VOS_1 | PWR_VOSR_BOOSTEN;
|
||||
while ((PWR->VOSR & PWR_VOSR_VOSRDY) == 0)
|
||||
spin(1);
|
||||
while ((PWR->SVMSR & PWR_SVMSR_ACTVOSRDY) == 0)
|
||||
spin(1);
|
||||
}
|
||||
PWR->DBPR |= PWR_DBPR_DBP; // enable backup access
|
||||
CLRSET(RCC->BDCR, 3 << 3, 0); // RCC_LSEDRIVE_LOW
|
||||
RCC->BDCR |= RCC_BDCR_LSEON; // LSE enable
|
||||
while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
|
||||
spin(1);
|
||||
RCC->BDCR |= RCC_BDCR_LSESYSEN; // LSESYS enable
|
||||
while ((RCC->BDCR & RCC_BDCR_LSESYSRDY) == 0)
|
||||
spin(1);
|
||||
PWR->VOSR &= ~PWR_VOSR_BOOSTEN;
|
||||
RCC->CR &= ~BIT(24); // configure PLL1
|
||||
while ((RCC->CR & RCC_CR_PLL1RDY) != 0)
|
||||
spin(1);
|
||||
RCC->PLL1CFGR = BIT(0) | ((PLL1_M - 1) << 8) | BIT(16) | BIT(17) |
|
||||
BIT(18); // source, PLL1_M, enable PLL1_P, PLL1_Q
|
||||
RCC->PLL1DIVR = (PLL1_N - 1) | ((PLL1_P - 1) << 9) | ((PLL1_Q - 1) << 16) |
|
||||
((PLL1_R - 1) << 24);
|
||||
PWR->VOSR |= PWR_VOSR_BOOSTEN;
|
||||
RCC->CR |= BIT(24); // PLL1ON
|
||||
while ((RCC->CR & RCC_CR_PLL1RDY) == 0)
|
||||
spin(1);
|
||||
CLRSET(FLASH->ACR, 0xf, 4); // flash latency 4
|
||||
RCC->CFGR1 |= 3; // set PLL as sysclk source
|
||||
CLRSET(RCC->CFGR2, 0xf, hal_ahb_div2prescval(HAL_HPRE)); // HPRE
|
||||
CLRSET(RCC->CFGR2, 0x70, hal_apb_div2prescval(HAL_D1PRE) << 4); // D1PRE
|
||||
CLRSET(RCC->CFGR2, 0x700, hal_apb_div2prescval(HAL_D2PRE) << 8); // D2PRE
|
||||
CLRSET(RCC->CFGR3, 0x70, hal_apb_div2prescval(HAL_D3PRE) << 4); // D3PRE
|
||||
}
|
||||
|
||||
static inline void hal_system_init(void) {
|
||||
SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); // Enable FPU
|
||||
__DSB();
|
||||
__ISB();
|
||||
|
||||
SCB->VTOR = FLASH_BASE; // Vector Table Relocation in Internal FLASH
|
||||
}
|
||||
|
||||
static inline void hal_spi_init(SPI_TypeDef *spi, uint16_t div) {
|
||||
(void)spi;
|
||||
uint32_t br = 0; // Baud rate prescaler: div = 2,4,8...256 -> MBR = 0..7
|
||||
while (div > 2)
|
||||
div >>= 1, br++;
|
||||
if (br > 7)
|
||||
br = 7;
|
||||
|
||||
CLRSET(RCC->CCIPR1, 3 << 20, 1); // SYSCLK source
|
||||
RCC->APB2ENR |= BIT(12); // SPI1EN
|
||||
spin(100);
|
||||
hal_gpio_init(PIN('A', 5), HAL_GPIO_MODE_AF, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_INSANE, HAL_GPIO_PULL_DOWN, 5); // SCK
|
||||
hal_gpio_init(PIN('A', 6), HAL_GPIO_MODE_AF, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_INSANE, HAL_GPIO_PULL_NONE, 5); // MISO
|
||||
hal_gpio_init(PIN('A', 7), HAL_GPIO_MODE_AF, HAL_GPIO_OTYPE_PUSH_PULL,
|
||||
HAL_GPIO_SPEED_INSANE, HAL_GPIO_PULL_NONE, 5); // MOSI
|
||||
hal_gpio_output(SPI_CS); // CS
|
||||
hal_gpio_output(SPI_CHIP_EN); // CHIP_EN
|
||||
hal_gpio_input(SPI_RDY); // RDY
|
||||
spi->CR1 = 0; // disable SPI
|
||||
spi->CR1 = SPI_CR1_SSI_Msk; // SSI
|
||||
spi->CFG1 = (7 << SPI_CFG1_DSIZE_Pos) | (br << SPI_CFG1_MBR_Pos); // DSIZE = 8-bit, MBR
|
||||
spi->CFG2 = SPI_CFG2_SSM_Msk | SPI_CFG2_MASTER_Msk;
|
||||
spi->CR1 |= SPI_CR1_SPE_Msk; // Enable SPI
|
||||
spi->CR1 |= SPI_CR1_CSTART_Msk; // Start transfer
|
||||
}
|
||||
|
||||
static inline uint8_t hal_spi_xfer_byte(SPI_TypeDef *spi, uint8_t byte) {
|
||||
(void)spi;
|
||||
while ((spi->SR & SPI_SR_TXP_Msk) == 0)
|
||||
spin(1); // TXP
|
||||
*(volatile uint8_t *) &spi->TXDR = byte;
|
||||
while (((spi->SR & SPI_SR_RXP_Msk) == 0) && ((spi->SR & SPI_SR_RXWNE_Msk) == 0) && ((spi->SR & SPI_SR_RXPLVL_Msk) == 0))
|
||||
spin(1); // RXP
|
||||
uint8_t rx = *(volatile uint8_t *) &spi->RXDR;
|
||||
return rx;
|
||||
}
|
||||
|
||||
static inline void hal_spi_tx_rx(SPI_TypeDef *spi, uint8_t *tx, uint8_t *rx, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
uint8_t rx_ = hal_spi_xfer_byte(spi, tx ? tx[i] : 0);
|
||||
if (rx) rx[i] = rx_;
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool hal_spi_ready(SPI_TypeDef *spi) {
|
||||
(void) spi;
|
||||
return hal_gpio_read(SPI_RDY);
|
||||
}
|
||||
|
||||
static inline void hal_spi_select(SPI_TypeDef *spi, bool s) {
|
||||
(void)spi;
|
||||
hal_gpio_write(SPI_CS, s);
|
||||
}
|
||||
|
||||
static inline void hal_spi_done(SPI_TypeDef *spi) {
|
||||
while((spi->SR & SPI_SR_TXC_Msk) == 0) spin(1);
|
||||
spi->IFCR = 0xffff; // clear all flags
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
ENTRY(Reset_Handler);
|
||||
MEMORY {
|
||||
flash(rx) : ORIGIN = 0x08000000, LENGTH = 4096k
|
||||
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
|
||||
}
|
||||
_estack = ORIGIN(sram) + LENGTH(sram); /* stack points to end of SRAM */
|
||||
|
||||
SECTIONS {
|
||||
.vectors : { KEEP(*(.isr_vector)) } > flash
|
||||
.text : { *(.text* .text.*) } > flash
|
||||
.rodata : { *(.rodata*) } > flash
|
||||
|
||||
.data : {
|
||||
_sdata = .;
|
||||
*(.first_data)
|
||||
*(.data SORT(.data.*))
|
||||
*(.iram .iram* .iram.*)
|
||||
_edata = .;
|
||||
} > sram AT > flash
|
||||
_sidata = LOADADDR(.data);
|
||||
|
||||
.bss : {
|
||||
_sbss = .;
|
||||
*(.bss SORT(.bss.*) COMMON)
|
||||
_ebss = .;
|
||||
} > sram
|
||||
|
||||
. = ALIGN(8);
|
||||
_end = .;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "mongoose.h"
|
||||
#include "hal.h"
|
||||
|
||||
#define DEBUG_UART USART1
|
||||
#define LED1 PIN('C', 7) // On-board LED pin (green)
|
||||
#define LED2 PIN('B', 7) // On-board LED pin (blue)
|
||||
#define LED3 PIN('G', 2) // On-board LED pin (red)
|
||||
|
||||
// Redirect stdout debug output to UART
|
||||
int _write(int fd, char *ptr, int len) {
|
||||
if (fd == 1 || fd == 2) hal_uart_write_buf(DEBUG_UART, ptr, (size_t) len);
|
||||
return len;
|
||||
}
|
||||
|
||||
static void blink_task(void) {
|
||||
static uint64_t blink_timer = 0;
|
||||
if (hal_timer_expired(&blink_timer, 500, hal_get_tick())) {
|
||||
hal_gpio_toggle(LED1);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return hal_get_tick();
|
||||
}
|
||||
|
||||
bool mg_random(void *buf, size_t len) {
|
||||
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
|
||||
uint32_t r = hal_rng_read();
|
||||
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) { // New HTTP request received
|
||||
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 {
|
||||
mg_http_reply(c, 200, "", "Hi from Mongoose, tick %llu\n",
|
||||
hal_get_tick());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
hal_clock_init();
|
||||
hal_rng_init();
|
||||
hal_gpio_output(LED1);
|
||||
hal_gpio_output(LED2);
|
||||
hal_gpio_output(LED3);
|
||||
hal_uart_init(DEBUG_UART, 115200);
|
||||
|
||||
extern void hwspecific_spi_init();
|
||||
hwspecific_spi_init();
|
||||
|
||||
// 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);
|
||||
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 0);
|
||||
blink_task();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
// See https://mongoose.ws/documentation/#build-options
|
||||
#define MG_ARCH MG_ARCH_ARMGCC
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
#define MG_ENABLE_DRIVER_ST67W6 1
|
||||
|
||||
// Set your Wi-Fi configuration using a custom function:
|
||||
extern void wifi_setconfig(void *data);
|
||||
#define MG_SET_WIFI_CONFIG(ifp, data) wifi_setconfig(data)
|
||||
@@ -0,0 +1,76 @@
|
||||
// SPDX-FileCopyrightText: 2026 Cesanta Software Limited
|
||||
// SPDX-License-Identifier: GPL-2.0-only or commercial
|
||||
// Generated by Mongoose Wizard, https://mongoose.ws/wizard/
|
||||
|
||||
#include "hal.h"
|
||||
#include "mongoose.h"
|
||||
|
||||
#ifndef WIFI_NAME
|
||||
#define WIFI_NAME ""
|
||||
#endif
|
||||
|
||||
#ifndef WIFI_PASS
|
||||
#define WIFI_PASS ""
|
||||
#endif
|
||||
|
||||
#ifndef WIFI_AP_NAME
|
||||
#define WIFI_AP_NAME "Mongoose"
|
||||
#endif
|
||||
|
||||
#ifndef WIFI_AP_PASS
|
||||
#define WIFI_AP_PASS "mongoose"
|
||||
#endif
|
||||
|
||||
#ifndef ENABLE_WIFI_AP
|
||||
#define ENABLE_WIFI_AP 1
|
||||
#endif
|
||||
|
||||
static bool hwspecific_is_ready(void) {
|
||||
return hal_gpio_read(SPI_RDY);
|
||||
}
|
||||
|
||||
static void hwspecific_spi_begin(void *arg) {
|
||||
hal_spi_select(WIFISPI, true);
|
||||
(void) arg;
|
||||
}
|
||||
|
||||
static void hwspecific_spi_txn(void *arg, uint8_t *txdata, uint8_t *rxdata, size_t len) {
|
||||
while (len--) {
|
||||
uint8_t rx = hal_spi_xfer_byte(WIFISPI, txdata != NULL ? *txdata++ : 0);
|
||||
if (rxdata != NULL) *rxdata++ = rx;
|
||||
}
|
||||
hal_spi_done(WIFISPI);
|
||||
(void) arg;
|
||||
}
|
||||
|
||||
static void hwspecific_spi_end(void *arg) {
|
||||
hal_spi_select(WIFISPI, false);
|
||||
(void) arg;
|
||||
}
|
||||
|
||||
void hwspecific_spi_init(void) {
|
||||
hal_gpio_output(SPI_CHIP_EN);
|
||||
hal_gpio_write(SPI_CHIP_EN, 0);
|
||||
hal_gpio_input(SPI_RDY);
|
||||
hal_spi_init(WIFISPI, 128); // runs at 1.25MHz (SYS_CLK/128)
|
||||
mg_delayms(10);
|
||||
hal_gpio_write(SPI_CHIP_EN, 1);
|
||||
}
|
||||
|
||||
static const struct mg_tcpip_spi spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
|
||||
|
||||
void wifi_setconfig(void *data) {
|
||||
struct mg_tcpip_driver_st67w6_data *d = (struct mg_tcpip_driver_st67w6_data *) data;
|
||||
d->wifi.ssid = WIFI_NAME;
|
||||
d->wifi.pass = WIFI_PASS;
|
||||
d->wifi.apssid = WIFI_AP_NAME;
|
||||
d->wifi.appass = WIFI_AP_PASS;
|
||||
d->wifi.apip = MG_IPV4(192, 168, 169, 1);
|
||||
d->wifi.apmask = MG_IPV4(255, 255, 255, 0);
|
||||
d->wifi.security = 0;
|
||||
d->wifi.apsecurity = 0;
|
||||
d->wifi.apchannel = 10;
|
||||
d->wifi.apmode = ENABLE_WIFI_AP;
|
||||
d->spi = (struct mg_tcpip_spi *) &spi;
|
||||
d->is_ready = hwspecific_is_ready;
|
||||
}
|
||||
Reference in new issue
Block a user