Skip to content

Commit 75c1143

Browse files
committed
Introduce MG_ARCH_CUBE, autodetect everything
1 parent e07a828 commit 75c1143

9 files changed

Lines changed: 186 additions & 56 deletions

File tree

mongoose.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,7 +3048,7 @@ long mg_json_get_long(struct mg_str json, const char *path, long dflt) {
30483048

30493049

30503050

3051-
int mg_log_level = MG_LL_INFO;
3051+
int mg_log_level = MG_LL_DEBUG;
30523052
static mg_pfn_t s_log_func = mg_pfn_stdout;
30533053
static void *s_log_func_param = NULL;
30543054

@@ -19779,6 +19779,13 @@ bool mg_random(void *buf, size_t len) {
1977919779
#if MG_ARCH == MG_ARCH_ESP32
1978019780
while (len--) *p++ = (unsigned char) (esp_random() & 255);
1978119781
success = true;
19782+
#elif MG_ARCH == MG_ARCH_CUBE && defined(HAL_RNG_MODULE_ENABLED)
19783+
extern RNG_HandleTypeDef hrng;
19784+
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
19785+
uint32_t r = HAL_RNG_ReadLastRandomNumber(&hrng);
19786+
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
19787+
}
19788+
success = true;
1978219789
#elif MG_ARCH == MG_ARCH_PICOSDK
1978319790
while (len--) *p++ = (unsigned char) (get_rand_32() & 255);
1978419791
success = true;
@@ -19909,6 +19916,8 @@ uint64_t mg_millis(void) {
1990919916
#elif MG_ARCH == MG_ARCH_ESP8266 || MG_ARCH == MG_ARCH_ESP32 || \
1991019917
MG_ARCH == MG_ARCH_FREERTOS
1991119918
return xTaskGetTickCount() * portTICK_PERIOD_MS;
19919+
#elif MG_ARCH == MG_ARCH_CUBE
19920+
return (uint64_t) HAL_GetTick();
1991219921
#elif MG_ARCH == MG_ARCH_THREADX
1991319922
return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
1991419923
#elif MG_ARCH == MG_ARCH_TIRTOS
@@ -19942,10 +19951,6 @@ uint64_t mg_millis(void) {
1994219951
return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000);
1994319952
#elif defined(ARDUINO)
1994419953
return (uint64_t) millis();
19945-
#elif defined(__STM32H5xx_HAL_H) || defined(__STM32H7xx_HAL_H) || \
19946-
defined(__STM32F7xx_HAL_H) || defined(__STM32F4xx_HAL_H) || \
19947-
defined(__STM32F2xx_HAL_H) || defined(__STM32F1xx_HAL_H)
19948-
return (uint64_t) HAL_GetTick(); // Using STM32 HAL
1994919954
#else
1995019955
return (uint64_t) (time(NULL) * 1000);
1995119956
#endif

mongoose.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern "C" {
8080

8181

8282

83+
8384
#if MG_ARCH == MG_ARCH_ARMCGT
8485

8586
#include <ctype.h>
@@ -122,6 +123,85 @@ extern "C" {
122123
#endif
123124

124125

126+
#if MG_ARCH == MG_ARCH_CUBE
127+
128+
#include <ctype.h>
129+
#include <errno.h>
130+
#include <stdarg.h>
131+
#include <stdbool.h>
132+
#include <stdio.h>
133+
#include <stdlib.h>
134+
#include <string.h>
135+
#include <sys/stat.h>
136+
#include <sys/time.h>
137+
#include <sys/types.h>
138+
#include <time.h>
139+
#include <unistd.h>
140+
141+
// Cube-generated header, includes ST Cube HAL
142+
// NOTE: use angle brackets to prevent amalgamator ditching it
143+
#include <main.h>
144+
145+
#ifndef MG_PATH_MAX
146+
#define MG_PATH_MAX 100
147+
#endif
148+
149+
#ifndef MG_ENABLE_DIRLIST
150+
#define MG_ENABLE_DIRLIST 0
151+
#endif
152+
153+
#ifndef MG_ENABLE_SOCKET
154+
#define MG_ENABLE_SOCKET 0
155+
#endif
156+
157+
#ifndef MG_ENABLE_TCPIP
158+
#define MG_ENABLE_TCPIP 1 // Enable built-in TCP/IP stack
159+
#endif
160+
161+
#if MG_ENABLE_TCPIP && !defined(MG_ENABLE_DRIVER_STM32F) && !defined(MG_ENABLE_DRIVER_STM32H)
162+
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32F7)
163+
#define MG_ENABLE_DRIVER_STM32F 1
164+
#elif defined(STM32H5) || defined(STM32H7)
165+
#define MG_ENABLE_DRIVER_STM32H 1
166+
#else
167+
#error Select a driver in mongoose_config.h
168+
#endif
169+
#endif
170+
171+
#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32F7)
172+
#define MG_OTA MG_OTA_STM32F
173+
#elif !defined(MG_OTA) && defined(STM32H5)
174+
#define MG_OTA MG_OTA_STM32H5
175+
#elif !defined(MG_OTA) && defined(STM32H7)
176+
#define MG_OTA MG_OTA_STM32H7
177+
#endif
178+
179+
#ifndef STM32H5
180+
#define HAL_ICACHE_IsEnabled() 0
181+
#define HAL_ICACHE_Enable() (void) 0
182+
#define HAL_ICACHE_Disable() (void) 0
183+
#endif
184+
185+
#ifndef MG_SET_MAC_ADDRESS
186+
// Construct MAC address from UUID
187+
#define MGUID ((uint32_t *) UID_BASE) // Unique 96-bit chip ID
188+
#define MG_SET_MAC_ADDRESS(mac) \
189+
do { \
190+
int icache_enabled_ = HAL_ICACHE_IsEnabled(); \
191+
if (icache_enabled_) HAL_ICACHE_Disable(); \
192+
mac[0] = 42; \
193+
mac[1] = ((MGUID[0] >> 0) & 255) ^ ((MGUID[2] >> 19) & 255); \
194+
mac[2] = ((MGUID[0] >> 10) & 255) ^ ((MGUID[1] >> 10) & 255); \
195+
mac[3] = (MGUID[0] >> 19) & 255; \
196+
mac[4] = ((MGUID[1] >> 0) & 255) ^ ((MGUID[2] >> 10) & 255); \
197+
mac[5] = ((MGUID[2] >> 0) & 255) ^ ((MGUID[1] >> 19) & 255); \
198+
if (icache_enabled_) HAL_ICACHE_Enable(); \
199+
} while (0)
200+
#endif
201+
202+
#endif
203+
204+
125205
#if MG_ARCH == MG_ARCH_ESP32
126206

127207
#include <ctype.h>
@@ -3419,6 +3499,7 @@ struct mg_tcpip_driver_stm32f_data {
34193499
mif_.driver_data = &driver_data_; \
34203500
MG_SET_MAC_ADDRESS(mif_.mac); \
34213501
mg_tcpip_init(mgr, &mif_); \
3502+
NVIC_EnableIRQ(ETH_IRQn); \
34223503
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
34233504
} while (0)
34243505

@@ -3479,6 +3560,7 @@ struct mg_tcpip_driver_stm32h_data {
34793560
mif_.driver_data = &driver_data_; \
34803561
MG_SET_MAC_ADDRESS(mif_.mac); \
34813562
mg_tcpip_init(mgr, &mif_); \
3563+
NVIC_EnableIRQ(ETH_IRQn); \
34823564
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
34833565
} while (0)
34843566

src/arch.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@
3838
// http://esr.ibiblio.org/?p=5095
3939
#define MG_BIG_ENDIAN (*(uint16_t *) "\0\xff" < 0x100)
4040

41+
#include "arch_armgcc.h"
42+
#include "arch_cube.h"
4143
#include "arch_esp32.h"
4244
#include "arch_esp8266.h"
4345
#include "arch_freertos.h"
44-
#include "arch_armgcc.h"
4546
#include "arch_rtx.h"
47+
#include "arch_threadx.h"
4648
#include "arch_unix.h"
4749
#include "arch_win32.h"
4850
#include "arch_zephyr.h"
49-
#include "arch_threadx.h"
5051

5152
#include "net_ft.h"
5253
#include "net_lwip.h"

src/arch_cube.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
3+
#if MG_ARCH == MG_ARCH_CUBE
4+
5+
#include <ctype.h>
6+
#include <errno.h>
7+
#include <stdarg.h>
8+
#include <stdbool.h>
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
#include <sys/stat.h>
13+
#include <sys/time.h>
14+
#include <sys/types.h>
15+
#include <time.h>
16+
#include <unistd.h>
17+
18+
// Cube-generated header, includes ST Cube HAL
19+
// NOTE: use angle brackets to prevent amalgamator ditching it
20+
#include <main.h>
21+
22+
#ifndef MG_PATH_MAX
23+
#define MG_PATH_MAX 100
24+
#endif
25+
26+
#ifndef MG_ENABLE_DIRLIST
27+
#define MG_ENABLE_DIRLIST 0
28+
#endif
29+
30+
#ifndef MG_ENABLE_SOCKET
31+
#define MG_ENABLE_SOCKET 0
32+
#endif
33+
34+
#ifndef MG_ENABLE_TCPIP
35+
#define MG_ENABLE_TCPIP 1 // Enable built-in TCP/IP stack
36+
#endif
37+
38+
#if MG_ENABLE_TCPIP && !defined(MG_ENABLE_DRIVER_STM32F) && !defined(MG_ENABLE_DRIVER_STM32H)
39+
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32F7)
40+
#define MG_ENABLE_DRIVER_STM32F 1
41+
#elif defined(STM32H5) || defined(STM32H7)
42+
#define MG_ENABLE_DRIVER_STM32H 1
43+
#else
44+
#error Select a driver in mongoose_config.h
45+
#endif
46+
#endif
47+
48+
#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32F7)
49+
#define MG_OTA MG_OTA_STM32F
50+
#elif !defined(MG_OTA) && defined(STM32H5)
51+
#define MG_OTA MG_OTA_STM32H5
52+
#elif !defined(MG_OTA) && defined(STM32H7)
53+
#define MG_OTA MG_OTA_STM32H7
54+
#endif
55+
56+
#ifndef STM32H5
57+
#define HAL_ICACHE_IsEnabled() 0
58+
#define HAL_ICACHE_Enable() (void) 0
59+
#define HAL_ICACHE_Disable() (void) 0
60+
#endif
61+
62+
#ifndef MG_SET_MAC_ADDRESS
63+
// Construct MAC address from UUID
64+
#define MGUID ((uint32_t *) UID_BASE) // Unique 96-bit chip ID
65+
#define MG_SET_MAC_ADDRESS(mac) \
66+
do { \
67+
int icache_enabled_ = HAL_ICACHE_IsEnabled(); \
68+
if (icache_enabled_) HAL_ICACHE_Disable(); \
69+
mac[0] = 42; \
70+
mac[1] = ((MGUID[0] >> 0) & 255) ^ ((MGUID[2] >> 19) & 255); \
71+
mac[2] = ((MGUID[0] >> 10) & 255) ^ ((MGUID[1] >> 10) & 255); \
72+
mac[3] = (MGUID[0] >> 19) & 255; \
73+
mac[4] = ((MGUID[1] >> 0) & 255) ^ ((MGUID[2] >> 10) & 255); \
74+
mac[5] = ((MGUID[2] >> 0) & 255) ^ ((MGUID[1] >> 19) & 255); \
75+
if (icache_enabled_) HAL_ICACHE_Enable(); \
76+
} while (0)
77+
#endif
78+
79+
#endif

src/drivers/stm32f.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct mg_tcpip_driver_stm32f_data {
4141
mif_.driver_data = &driver_data_; \
4242
MG_SET_MAC_ADDRESS(mif_.mac); \
4343
mg_tcpip_init(mgr, &mif_); \
44+
NVIC_EnableIRQ(ETH_IRQn); \
4445
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
4546
} while (0)
4647

src/drivers/stm32h.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct mg_tcpip_driver_stm32h_data {
5454
mif_.driver_data = &driver_data_; \
5555
MG_SET_MAC_ADDRESS(mif_.mac); \
5656
mg_tcpip_init(mgr, &mif_); \
57+
NVIC_EnableIRQ(ETH_IRQn); \
5758
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
5859
} while (0)
5960

src/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "str.h"
44
#include "util.h"
55

6-
int mg_log_level = MG_LL_INFO;
6+
int mg_log_level = MG_LL_DEBUG;
77
static mg_pfn_t s_log_func = mg_pfn_stdout;
88
static void *s_log_func_param = NULL;
99

src/util.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ bool mg_random(void *buf, size_t len) {
1717
#if MG_ARCH == MG_ARCH_ESP32
1818
while (len--) *p++ = (unsigned char) (esp_random() & 255);
1919
success = true;
20+
#elif MG_ARCH == MG_ARCH_CUBE && defined(HAL_RNG_MODULE_ENABLED)
21+
extern RNG_HandleTypeDef hrng;
22+
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
23+
uint32_t r = HAL_RNG_ReadLastRandomNumber(&hrng);
24+
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
25+
}
26+
success = true;
2027
#elif MG_ARCH == MG_ARCH_PICOSDK
2128
while (len--) *p++ = (unsigned char) (get_rand_32() & 255);
2229
success = true;
@@ -147,6 +154,8 @@ uint64_t mg_millis(void) {
147154
#elif MG_ARCH == MG_ARCH_ESP8266 || MG_ARCH == MG_ARCH_ESP32 || \
148155
MG_ARCH == MG_ARCH_FREERTOS
149156
return xTaskGetTickCount() * portTICK_PERIOD_MS;
157+
#elif MG_ARCH == MG_ARCH_CUBE
158+
return (uint64_t) HAL_GetTick();
150159
#elif MG_ARCH == MG_ARCH_THREADX
151160
return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
152161
#elif MG_ARCH == MG_ARCH_TIRTOS
@@ -180,10 +189,6 @@ uint64_t mg_millis(void) {
180189
return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000);
181190
#elif defined(ARDUINO)
182191
return (uint64_t) millis();
183-
#elif defined(__STM32H5xx_HAL_H) || defined(__STM32H7xx_HAL_H) || \
184-
defined(__STM32F7xx_HAL_H) || defined(__STM32F4xx_HAL_H) || \
185-
defined(__STM32F2xx_HAL_H) || defined(__STM32F1xx_HAL_H)
186-
return (uint64_t) HAL_GetTick(); // Using STM32 HAL
187192
#else
188193
return (uint64_t) (time(NULL) * 1000);
189194
#endif

test/configs/mongoose_config_stm32.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)