Skip to content

Commit cb1ae31

Browse files
authored
Merge pull request #2404 from cesanta/g031
Add nucleo-g031 baremetal example
2 parents 79d6017 + 4db3fc2 commit cb1ae31

File tree

10 files changed

+1708
-1488
lines changed

10 files changed

+1708
-1488
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
CFLAGS = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion
2+
CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion
3+
CFLAGS += -g3 -O3 -ffunction-sections -fdata-sections
4+
CFLAGS += -I. -Icmsis_core/CMSIS/Core/Include -Icmsis_g0/Include
5+
CFLAGS += -mcpu=cortex-m0 -mthumb -mfloat-abi=soft $(CFLAGS_EXTRA)
6+
LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map
7+
8+
SOURCES = main.c syscalls.c mongoose.c
9+
SOURCES += cmsis_g0/Source/Templates/gcc/startup_stm32g031xx.s # ST startup file. Compiler-dependent!
10+
CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" # Example-specifig flags
11+
12+
ifeq ($(OS),Windows_NT)
13+
RM = cmd /C del /Q /F /S
14+
else
15+
RM = rm -rf
16+
endif
17+
18+
all build example: firmware.bin
19+
20+
firmware.bin: firmware.elf
21+
arm-none-eabi-objcopy -O binary $< $@
22+
23+
firmware.elf: cmsis_core cmsis_g0 $(SOURCES) hal.h link.ld Makefile mongoose_custom.h
24+
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
25+
26+
flash: firmware.bin
27+
st-flash --reset write $< 0x8000000
28+
29+
cmsis_core: # ARM CMSIS core headers
30+
git clone --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@
31+
cmsis_g0: # ST CMSIS headers for STM32H5 series
32+
git clone --depth 1 -b v1.4.3 https://github.com/STMicroelectronics/cmsis_device_g0 $@
33+
34+
# Automated remote test. Requires env variable VCON_API_KEY set. See https://vcon.io/automated-firmware-tests/
35+
DEVICE_URL ?= https://dash.vcon.io/api/v3/devices/10
36+
update: firmware.bin
37+
curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/ota --data-binary @$<
38+
39+
test update: CFLAGS += -DUART_DEBUG=USART1
40+
test: update
41+
curl --fail-with-body -su :$(VCON_API_KEY) $(DEVICE_URL)/tx?t=5 | tee /tmp/output.txt
42+
grep 'READY, IP:' /tmp/output.txt # Check for network init
43+
44+
clean:
45+
$(RM) firmware.* *.su cmsis_core cmsis_g0 mbedtls
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Copyright (c) 2022-2023 Cesanta Software Limited
2+
// All rights reserved
3+
//
4+
// MCU manual: RM0444, board manual: UM2591
5+
// https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
6+
// https://www.st.com/resource/en/user_manual/um2591-stm32g0-nucleo32-board-mb1455-stmicroelectronics.pdf
7+
// Alternate functions: https://www.st.com/resource/en/datasheet/stm32g031c6.pdf
8+
9+
#pragma once
10+
11+
// #define LED PIN('B', 3)
12+
#define LED PIN('C', 6)
13+
#ifndef UART_DEBUG
14+
#define UART_DEBUG USART2
15+
#endif
16+
17+
#include <stm32g031xx.h>
18+
19+
#include <stdbool.h>
20+
#include <stdint.h>
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <string.h>
24+
25+
#define BIT(x) (1UL << (x))
26+
#define SETBITS(R, CLEARMASK, SETMASK) (R) = ((R) & ~(CLEARMASK)) | (SETMASK)
27+
#define PIN(bank, num) ((((bank) - 'A') << 8) | (num))
28+
#define PINNO(pin) (pin & 255)
29+
#define PINBANK(pin) (pin >> 8)
30+
31+
#define CPU_FREQUENCY 16000000
32+
#define AHB_FREQUENCY CPU_FREQUENCY
33+
#define APB_FREQUENCY CPU_FREQUENCY
34+
// #define APB1_FREQUENCY (AHB_FREQUENCY / (BIT(PPRE1 - 3)))
35+
36+
static inline void spin(volatile uint32_t n) {
37+
while (n--) (void) 0;
38+
}
39+
40+
enum { GPIO_MODE_INPUT, GPIO_MODE_OUTPUT, GPIO_MODE_AF, GPIO_MODE_ANALOG };
41+
enum { GPIO_OTYPE_PUSH_PULL, GPIO_OTYPE_OPEN_DRAIN };
42+
enum { GPIO_SPEED_LOW, GPIO_SPEED_MEDIUM, GPIO_SPEED_HIGH, GPIO_SPEED_INSANE };
43+
enum { GPIO_PULL_NONE, GPIO_PULL_UP, GPIO_PULL_DOWN };
44+
45+
#define GPIO(N) ((GPIO_TypeDef *) ((GPIOA_BASE) + 0x400 * (N)))
46+
47+
static GPIO_TypeDef *gpio_bank(uint16_t pin) {
48+
return GPIO(PINBANK(pin));
49+
}
50+
static inline void gpio_toggle(uint16_t pin) {
51+
GPIO_TypeDef *gpio = gpio_bank(pin);
52+
uint32_t mask = BIT(PINNO(pin));
53+
gpio->BSRR = mask << (gpio->ODR & mask ? 16 : 0);
54+
}
55+
static inline int gpio_read(uint16_t pin) {
56+
return gpio_bank(pin)->IDR & BIT(PINNO(pin)) ? 1 : 0;
57+
}
58+
static inline void gpio_write(uint16_t pin, bool val) {
59+
GPIO_TypeDef *gpio = gpio_bank(pin);
60+
gpio->BSRR = BIT(PINNO(pin)) << (val ? 0 : 16);
61+
}
62+
static inline void gpio_init(uint16_t pin, uint8_t mode, uint8_t type,
63+
uint8_t speed, uint8_t pull, uint8_t af) {
64+
GPIO_TypeDef *gpio = gpio_bank(pin);
65+
uint8_t n = (uint8_t) (PINNO(pin));
66+
RCC->IOPENR |= BIT(PINBANK(pin)); // Enable GPIO clock
67+
SETBITS(gpio->OTYPER, 1UL << n, ((uint32_t) type) << n);
68+
SETBITS(gpio->OSPEEDR, 3UL << (n * 2), ((uint32_t) speed) << (n * 2));
69+
SETBITS(gpio->PUPDR, 3UL << (n * 2), ((uint32_t) pull) << (n * 2));
70+
SETBITS(gpio->AFR[n >> 3], 15UL << ((n & 7) * 4),
71+
((uint32_t) af) << ((n & 7) * 4));
72+
SETBITS(gpio->MODER, 3UL << (n * 2), ((uint32_t) mode) << (n * 2));
73+
}
74+
static inline void gpio_input(uint16_t pin) {
75+
gpio_init(pin, GPIO_MODE_INPUT, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH,
76+
GPIO_PULL_NONE, 0);
77+
}
78+
static inline void gpio_output(uint16_t pin) {
79+
gpio_init(pin, GPIO_MODE_OUTPUT, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH,
80+
GPIO_PULL_NONE, 0);
81+
}
82+
83+
static inline bool uart_init(USART_TypeDef *uart, unsigned long baud) {
84+
uint8_t af = 1; // Alternate function
85+
uint16_t rx = 0, tx = 0; // pins
86+
uint32_t freq = 0; // Bus frequency
87+
88+
if (uart == USART1) {
89+
freq = CPU_FREQUENCY, RCC->APBENR2 |= RCC_APBENR2_USART1EN;
90+
tx = PIN('A', 9), rx = PIN('A', 10);
91+
} else if (uart == USART2) {
92+
freq = CPU_FREQUENCY, RCC->APBENR1 |= RCC_APBENR1_USART2EN;
93+
tx = PIN('A', 2), rx = PIN('A', 3);
94+
} else {
95+
return false;
96+
}
97+
gpio_init(tx, GPIO_MODE_AF, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH, 0, af);
98+
gpio_init(rx, GPIO_MODE_AF, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH, 0, af);
99+
uart->CR1 = 0; // Disable UART
100+
uart->BRR = freq / baud; // Set baud rate
101+
uart->CR1 = USART_CR1_RE | USART_CR1_TE; // Set mode to TX & RX
102+
uart->CR1 |= USART_CR1_UE; // Enable UART
103+
return true;
104+
}
105+
static inline void uart_write_byte(USART_TypeDef *uart, uint8_t byte) {
106+
uart->TDR = byte;
107+
while ((uart->ISR & BIT(7)) == 0) spin(1);
108+
}
109+
static inline void uart_write_buf(USART_TypeDef *uart, char *buf, size_t len) {
110+
while (len-- > 0) uart_write_byte(uart, *(uint8_t *) buf++);
111+
}
112+
static inline int uart_read_ready(USART_TypeDef *uart) {
113+
return uart->ISR & BIT(5); // If RXNE bit is set, data is ready
114+
}
115+
static inline uint8_t uart_read_byte(USART_TypeDef *uart) {
116+
return (uint8_t) (uart->RDR & 255);
117+
}
118+
119+
#ifndef RNG
120+
struct rng {
121+
volatile uint32_t CR, SR, DR;
122+
};
123+
#define RNG ((struct rng *) 0x40025000) // RM0444 2.2.2 Table 6
124+
#endif
125+
126+
static inline void rng_init(void) {
127+
RCC->CCIPR |= 2U << 26U; // RNG clock source. Documented in 5.4.21
128+
RCC->AHBENR |= BIT(18); // RM0444 5.4.25 Table 36
129+
RNG->CR |= BIT(2); // 19.7.1
130+
}
131+
static inline uint32_t rng_read(void) {
132+
while ((RNG->SR & BIT(0)) == 0) (void) 0;
133+
return RNG->DR;
134+
}
135+
136+
// Bit-bang SPI implementation
137+
struct spi {
138+
uint16_t miso, mosi, clk, cs; // Pins
139+
int spin; // Number of NOP spins for bitbanging
140+
};
141+
142+
static inline void spi_begin(struct spi *spi) {
143+
gpio_write(spi->cs, 0);
144+
// printf("%s\n", __func__);
145+
}
146+
147+
static inline void spi_end(struct spi *spi) {
148+
gpio_write(spi->cs, 1);
149+
// printf("%s\n", __func__);
150+
}
151+
152+
static inline void spi_init(struct spi *spi) {
153+
gpio_input(spi->miso);
154+
gpio_output(spi->mosi);
155+
gpio_output(spi->clk);
156+
gpio_output(spi->cs);
157+
gpio_write(spi->cs, 1);
158+
// printf("%s\n", __func__);
159+
}
160+
161+
// Send a byte, and return a received byte
162+
static inline uint8_t spi_txn(struct spi *spi, uint8_t write_byte) {
163+
unsigned count = spi->spin <= 0 ? 9 : (unsigned) spi->spin;
164+
uint8_t rx = 0, tx = write_byte;
165+
for (int i = 0; i < 8; i++) {
166+
gpio_write(spi->mosi, tx & 0x80U); // Set mosi
167+
gpio_write(spi->clk, 1); // Clock high
168+
spin(count); // Wait half cycle
169+
rx <<= 1U; // Shift alreay read bits
170+
if (gpio_read(spi->miso)) rx |= 1U; // Read next bit
171+
gpio_write(spi->clk, 0); // Clock low
172+
spin(count); // Wait half cycle
173+
tx <<= 1U; // Discard written bit
174+
}
175+
// printf("%s %02x %02x\n", __func__, (int) write_byte, (int) rx);
176+
return rx; // Return the received byte
177+
}
178+
179+
#define UUID ((uint32_t *) UID_BASE) // Unique 96-bit chip ID. TRM 59.1
180+
181+
// Helper macro for MAC generation, byte reads not allowed
182+
#define GENERATE_LOCALLY_ADMINISTERED_MAC() \
183+
{ \
184+
2, UUID[0] & 255, (UUID[0] >> 10) & 255, (UUID[0] >> 19) & 255, \
185+
UUID[1] & 255, UUID[2] & 255 \
186+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ENTRY(Reset_Handler);
2+
MEMORY {
3+
flash(rx) : ORIGIN = 0x08000000, LENGTH = 64k
4+
sram(rwx) : ORIGIN = 0x20000000, LENGTH = 8k
5+
}
6+
_estack = ORIGIN(sram) + LENGTH(sram); /* End of RAM. stack points here */
7+
8+
SECTIONS {
9+
.vectors : { KEEP(*(.isr_vector)) } > flash
10+
.text : { *(.text* .text.*) } > flash
11+
.rodata : { *(.rodata*) } > flash
12+
13+
.data : {
14+
_sdata = .;
15+
*(.first_data)
16+
*(.ram)
17+
*(.data SORT(.data.*))
18+
_edata = .;
19+
} > sram AT > flash
20+
_sidata = LOADADDR(.data);
21+
22+
.bss : {
23+
_sbss = .;
24+
*(.bss SORT(.bss.*) COMMON)
25+
_ebss = .;
26+
} > sram
27+
28+
. = ALIGN(8);
29+
_end = .;
30+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (c) 2023 Cesanta Software Limited
2+
// All rights reserved
3+
4+
#include "hal.h"
5+
#include "mongoose.h"
6+
7+
#define BLINK_PERIOD_MS 1000 // LED blinking period in millis
8+
static struct spi spi_pins = {
9+
.miso = PIN('B', 4),
10+
.mosi = PIN('A', 0),
11+
.clk = PIN('A', 1),
12+
.cs = PIN('A', 4),
13+
};
14+
uint32_t SystemCoreClock = CPU_FREQUENCY;
15+
16+
void SystemInit(void) { // Called automatically by startup code
17+
rng_init();
18+
SysTick_Config(CPU_FREQUENCY / 1000); // Sys tick every 1ms
19+
}
20+
21+
static volatile uint64_t s_ticks; // Milliseconds since boot
22+
void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms
23+
s_ticks++;
24+
}
25+
26+
void mg_random(void *buf, size_t len) { // Use on-board RNG
27+
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
28+
uint32_t r = rng_read();
29+
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
30+
}
31+
}
32+
33+
uint64_t mg_millis(void) { // Let Mongoose use our uptime function
34+
return s_ticks; // Return number of milliseconds since boot
35+
}
36+
37+
static void timer_fn(void *arg) {
38+
gpio_toggle(LED); // Blink LED
39+
struct mg_tcpip_if *ifp = arg; // And show
40+
MG_INFO(("Ethernet: %d, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", ifp->state,
41+
mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent, ifp->ndrop,
42+
ifp->nerr));
43+
}
44+
45+
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
46+
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) fn_data;
47+
if (ev == MG_EV_HTTP_MSG) {
48+
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
49+
if (mg_http_match_uri(hm, "/api/hello")) { // Request to /api/hello
50+
mg_http_reply(c, 200, "", "{%m:%u,%m:%u,%m:%u,%m:%u,%m:%u}\n",
51+
MG_ESC("eth"), ifp->state, MG_ESC("frames_received"),
52+
ifp->nrecv, MG_ESC("frames_sent"), ifp->nsent,
53+
MG_ESC("frames_dropped"), ifp->ndrop,
54+
MG_ESC("interface_errors"), ifp->nerr);
55+
} else if (mg_http_match_uri(hm, "/")) { // Index page
56+
mg_http_reply(
57+
c, 200, "", "%s",
58+
"<html><head><link rel='icon' href='data:;base64,='></head><body>"
59+
"<h1>Welcome to Mongoose</h1>"
60+
"See <a href=/api/hello>/api/hello</a> for REST example"
61+
"</body></html>");
62+
} else { // All other URIs
63+
mg_http_reply(c, 404, "", "Not Found\n");
64+
}
65+
}
66+
}
67+
68+
int main(void) {
69+
gpio_output(LED); // Setup green LED
70+
uart_init(UART_DEBUG, 115200); // Initialise debug printf
71+
72+
// ethernet_init(); // Initialise ethernet pins
73+
MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));
74+
75+
struct mg_mgr mgr; // Initialise
76+
mg_mgr_init(&mgr); // Mongoose event manager
77+
mg_log_set(MG_LL_DEBUG); // Set log level
78+
79+
// Initialise Mongoose network stack
80+
spi_init(&spi_pins);
81+
struct mg_tcpip_spi spi = {
82+
.begin = (void (*)(void *)) spi_begin,
83+
.end = (void (*)(void *)) spi_end,
84+
.txn = (uint8_t(*)(void *, uint8_t)) spi_txn,
85+
.spi = &spi_pins,
86+
};
87+
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
88+
// Uncomment below for static configuration:
89+
// .ip = mg_htonl(MG_U32(192, 168, 0, 223)),
90+
// .mask = mg_htonl(MG_U32(255, 255, 255, 0)),
91+
// .gw = mg_htonl(MG_U32(192, 168, 0, 1)),
92+
.driver = &mg_tcpip_driver_w5500,
93+
.driver_data = &spi};
94+
mg_tcpip_init(&mgr, &mif);
95+
96+
MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
97+
while (mif.state != MG_TCPIP_STATE_READY) {
98+
mg_mgr_poll(&mgr, 0);
99+
}
100+
101+
MG_INFO(("Initialising application..."));
102+
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);
103+
mg_http_listen(&mgr, "http://0.0.0.0:80", fn, &mif);
104+
105+
MG_INFO(("Starting event loop"));
106+
for (;;) {
107+
mg_mgr_poll(&mgr, 0);
108+
}
109+
110+
return 0;
111+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../mongoose.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../mongoose.h
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#define MG_ARCH MG_ARCH_NEWLIB
4+
5+
#define MG_ENABLE_TCPIP 1
6+
#define MG_ENABLE_CUSTOM_MILLIS 1
7+
#define MG_ENABLE_CUSTOM_RANDOM 1
8+
#define MG_IO_SIZE 128
9+
#define MG_ENABLE_LINES 1

0 commit comments

Comments
 (0)