Compare commits

..
12 Commits
Author SHA1 Message Date
Sergio R. Caprile 63ba221588 map DSCP into capable L2s 2026-12-04 12:49:46 -03:00
Sergey Lyubka f3189519d8 Merge pull request #3816 from cesanta/flash
SPI flash support, with xiao nrf52 and portenta-h7
2026-12-03 12:46:50 +00:00
Sergio R. Caprile f03c00a516 Merge pull request #3813 from cesanta/tight
tighten MQTT and HTTP handlers
2026-12-03 09:38:58 -03:00
cpq f02ea9859e SPI flash support, with xiao nrf52 and portenta-h7 2026-12-03 11:56:57 +00:00
Sergey Lyubka d7a756a24a Merge pull request #3815 from cesanta/flash1
Introduce struct mg_flash::{read_fn,init_fn}
2026-12-03 11:39:41 +00:00
cpq 57e30ab2e9 Introduce struct mg_flash::{read_fn,init_fn} 2026-12-03 10:47:45 +00:00
Sergey Lyubka 9ac1106e74 Merge pull request #3814 from cesanta/spi
Rename mg_tcpip_spi to mg_spi and move it to periph.h
2026-12-03 09:45:53 +00:00
Sergio R. Caprile 5c4d06552c Merge pull request #3805 from cesanta/nono
remove PHY_TX/RX
2026-12-02 15:16:39 -03:00
Sergio R. Caprile 39bfa22037 remove PHY_TX/RX 2026-12-02 15:11:54 -03:00
Sergio R. Caprile 24f5519463 tighten MQTT and HTTP handlers 2026-12-02 14:31:43 -03:00
Sergio R. Caprile 948f16bab3 Merge pull request #3803 from cesanta/pplace
move timer stuff out of sntp/util
2026-12-02 14:31:26 -03:00
Sergio R. Caprile 48a6b0d422 move timer stuff out of sntp/util 2026-11-30 12:41:06 -03:00
49 changed files with 1165 additions and 578 deletions

No files matched your search

+407 -236
View File
File diff suppressed because it is too large. Load diff
+73 -35
View File
@@ -1807,6 +1807,33 @@ void mg_log_set_fn(mg_pfn_t fn, void *param);
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
struct mg_timer { struct mg_timer {
uint64_t period_ms; // Timer period in milliseconds uint64_t period_ms; // Timer period in milliseconds
uint64_t expire; // Expiration timestamp in milliseconds uint64_t expire; // Expiration timestamp in milliseconds
@@ -1926,6 +1953,8 @@ struct dirent {
}; };
typedef struct mg_lfs_fd DIR; typedef struct mg_lfs_fd DIR;
bool mg_lfs_init(size_t size);
#endif #endif
@@ -3475,20 +3504,12 @@ size_t mg_ws_wrap(struct mg_connection *, size_t len, int op);
size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...); size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...);
size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
va_list *); va_list *);
// Time / SNTP functions // SNTP functions
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
// updated automatically by mg_sntp_connect() on each successful response. // updated automatically by mg_sntp_connect() on each successful response.
@@ -3498,24 +3519,6 @@ extern uint64_t mg_boot_timestamp_ms;
// Until a successful SNTP request completes, this is identical to mg_millis(). // Until a successful SNTP request completes, this is identical to mg_millis().
uint64_t mg_now(void); uint64_t mg_now(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
// Connect to an SNTP server and send a time request. // Connect to an SNTP server and send a time request.
// `url` defaults to "udp://time.google.com:123" when NULL. // `url` defaults to "udp://time.google.com:123" when NULL.
// On success the internal boot timestamp is updated so that mg_now() returns // On success the internal boot timestamp is updated so that mg_now() returns
@@ -4279,6 +4282,13 @@ bool mg_ota_write(const void *buf, size_t len);
// Ends the OTA process and commits the written firmware. // Ends the OTA process and commits the written firmware.
bool mg_ota_end(void); bool mg_ota_end(void);
// Same as above, for a given flash. OTA backends use these to implement
// mg_ota_begin(), mg_ota_write() and mg_ota_end()
struct mg_flash;
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
bool mg_ota_flash_end(struct mg_flash *flash);
// Push-based OTA: mg_http_stream_body() callback that flashes the request // Push-based OTA: mg_http_stream_body() callback that flashes the request
// //
// ```c // ```c
@@ -4637,8 +4647,10 @@ void mg_mdash_free(struct mg_mgr *);
// SPI bus abstraction for SPI-attached chips, e.g. W5500. // SPI bus abstraction for SPI-attached chips, e.g. W5500 or SPI flash, see
// Network drivers get it via the driver_data field // mg_spi_flash_init(). Network drivers get it via the driver_data field.
// Callbacks get the spi field. txn() with NULL write sends filler bytes, with
// NULL read discards what it receives
struct mg_spi { struct mg_spi {
void *spi; // Opaque SPI bus handle passed to all callbacks void *spi; // Opaque SPI bus handle passed to all callbacks
void (*begin)(void *); // Assert chip-select, start of transaction void (*begin)(void *); // Assert chip-select, start of transaction
@@ -4653,21 +4665,49 @@ extern struct mg_spi mg_spi;
struct mg_flash { struct mg_flash {
void *start; // Address at which flash starts void *start; // Address at which flash starts
size_t size; // Flash size size_t size; // Flash size
size_t secsz; // Sector size size_t secsz; // Sector size
size_t align; // Write alignment size_t align; // Write alignment
bool (*read_fn)(void *, void *, size_t); // Read function
bool (*write_fn)(void *, const void *, size_t); // Write function bool (*write_fn)(void *, const void *, size_t); // Write function
bool (*swap_fn)(void); // Swap partitions bool (*swap_fn)(void); // Swap partitions
bool (*init_fn)(struct mg_flash *); // Optional, fills runtime attributes
}; };
extern struct mg_flash *mg_flash; extern struct mg_flash *mg_flash;
bool mg_lfs_init(size_t size);
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash); // Reads memory-mapped (XIP) flash. Use as read_fn for such flash
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash); bool mg_flash_read_xip(void *addr, void *buf, size_t len);
bool mg_ota_flash_end(struct mg_flash *flash);
// JEDEC SPI NOR flash: W25Q, P25Q, MX25 and similar, up to 16 MB, 3-byte
// addresses, single-line SPI. MG_SPI_FLASH() wraps these into a struct mg_flash
bool mg_spi_flash_init(struct mg_spi *spi, size_t *size);
bool mg_spi_flash_read(struct mg_spi *spi, size_t ofs, void *buf, size_t len);
bool mg_spi_flash_write(struct mg_spi *spi, size_t ofs, const void *buf,
size_t len);
// Defines struct mg_flash name for a JEDEC SPI NOR chip on the struct mg_spi
// pointer spi. Start is 0, so addresses are flash offsets. The size comes
// from the chip on first use, and stays 0 if there is no chip. For example:
// static struct mg_spi s_spi = {NULL, my_begin, my_end, my_txn};
// MG_SPI_FLASH(my_flash, &s_spi);
// #define MG_LFS_FLASH (&my_flash) // In mongoose_config.h
#define MG_SPI_FLASH(name, spi) \
static bool name##_init(struct mg_flash *f) { \
if (f->size == 0) mg_spi_flash_init(spi, &f->size); \
return f->size > 0; \
} \
static bool name##_read(void *a, void *b, size_t n) { \
return mg_spi_flash_read(spi, (size_t) a, b, n); \
} \
static bool name##_write(void *a, const void *b, size_t n) { \
return mg_spi_flash_write(spi, (size_t) a, b, n); \
} \
struct mg_flash name = {NULL, 0, 4096, 1, name##_read, \
name##_write, NULL, name##_init}
@@ -4811,8 +4851,6 @@ enum {
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connection failed driver and chip specific MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connection failed driver and chip specific
MG_TCPIP_EV_DRIVER, // Driver-defined event driver specific MG_TCPIP_EV_DRIVER, // Driver-defined event driver specific
MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6) MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6)
MG_TCPIP_EV_PHY_RX, // Radio frame received struct mg_str *
MG_TCPIP_EV_PHY_TX, // Radio frame about to be sent struct mg_str *
MG_TCPIP_EV_PHY_INIT, // PHY initialised struct mg_phy * MG_TCPIP_EV_PHY_INIT, // PHY initialised struct mg_phy *
MG_TCPIP_EV_USER // Starting ID for user-defined events MG_TCPIP_EV_USER // Starting ID for user-defined events
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
#include "net_builtin.h" #include "net_builtin.h"
#include "log.h" #include "log.h"
#include "sntp.h" #include "timer.h"
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD
+1 -1
View File
@@ -225,7 +225,7 @@ static size_t mg_cyw_tx(unsigned int ifc, void *data, size_t len) {
uint16_t txlen = (uint16_t) (len + sizeof(*hdr)); uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
memset(txdata, 0, sizeof(*hdr)); memset(txdata, 0, sizeof(*hdr));
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len); memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
// TODO(): hdr->bdc.priority = map IP to TOS if supporting QoS/ToS // TODO(): hdr->bdc.priority = dscp >> 3; // RFC-9435 5.1.1
hdr->bdc.flags = 2 << 4; // BDC version 2 hdr->bdc.flags = 2 << 4; // BDC version 2
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
// hdr->bdc.data_offset = 0; // actually zeroed above // hdr->bdc.data_offset = 0; // actually zeroed above
+3 -7
View File
@@ -206,11 +206,9 @@ static void mgi_radio_frame_header(uint8_t *frame, uint16_t dst, uint16_t src,
frame[MGI_RADIO_HDR_LEN] = type, frame[MGI_RADIO_HDR_LEN + 1] = burst; frame[MGI_RADIO_HDR_LEN] = type, frame[MGI_RADIO_HDR_LEN + 1] = burst;
} }
// Hand a radio frame to the hardware. The interface's fn sees it first // Hand a radio frame to the hardware.
static bool mgi_radio_send(struct mg_radio_node *node, const void *frame, static bool mgi_radio_send(struct mg_radio_node *node, const void *frame,
size_t len) { size_t len) {
struct mg_str s = mg_str_n((const char *) frame, len);
mg_tcpip_call(node->ifp, MG_TCPIP_EV_PHY_TX, &s);
return node->hw_tx(node->ifp, frame, len) == len; return node->hw_tx(node->ifp, frame, len) == len;
} }
@@ -731,12 +729,10 @@ bool mg_radio_poll(struct mg_tcpip_if *ifp, bool s1) {
while (node->rx_len == 0 && while (node->rx_len == 0 &&
(len = mg_queue_next(&node->frames, &record)) != 0) { (len = mg_queue_next(&node->frames, &record)) != 0) {
if (len > MGI_RX_STAMP_LEN) { if (len > MGI_RX_STAMP_LEN) {
struct mg_str s =
mg_str_n(record + MGI_RX_STAMP_LEN, len - MGI_RX_STAMP_LEN);
uint64_t arrived; uint64_t arrived;
memcpy(&arrived, record, MGI_RX_STAMP_LEN); memcpy(&arrived, record, MGI_RX_STAMP_LEN);
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_RX, &s); mgi_radio_input(node, arrived, record + MGI_RX_STAMP_LEN,
mgi_radio_input(node, arrived, s.buf, s.len); len - MGI_RX_STAMP_LEN);
} }
mg_queue_del(&node->frames, len); mg_queue_del(&node->frames, len);
} }
+100 -88
View File
@@ -1,98 +1,110 @@
#include "arch.h"
#include "flash.h" #include "flash.h"
#include "log.h" #include "log.h"
#include "ota.h"
#include "util.h" #include "util.h"
#ifdef MG_OTA_PUBLIC_KEY
#include "sha256.h"
#include "tls_p256.h"
#endif
#if MG_OTA != MG_OTA_NONE && MG_OTA != MG_OTA_CUSTOM // Read function for memory-mapped (XIP) flash, usable as mg_flash read_fn
bool mg_flash_read_xip(void *addr, void *buf, size_t len) {
static char *s_addr; // Current address to write to memmove(buf, addr, len);
static size_t s_size; // Firmware size to flash. In-progress indicator return true;
static uint32_t s_crc32; // Firmware checksum
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash) {
bool ok = false;
if (s_size) {
MG_ERROR(("OTA already in progress. Call mg_ota_end()"));
} else {
size_t half = flash->size / 2;
s_crc32 = 0;
s_addr = (char *) flash->start + half;
MG_DEBUG(("FW %lu bytes, max %lu", new_firmware_size, half));
if (new_firmware_size < half) {
ok = true;
s_size = new_firmware_size;
MG_INFO(("Starting OTA, firmware size %lu", s_size));
} else {
MG_ERROR(("Firmware %lu is too big to fit %lu", new_firmware_size, half - flash->align));
}
}
return ok;
} }
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) { // JEDEC SPI NOR commands
bool ok = false; #define MG_SPI_FLASH_WREN 0x06 // Write enable
if (s_size == 0) { #define MG_SPI_FLASH_RDSR 0x05 // Read status register: bit 0 busy, 1 WEL
MG_ERROR(("OTA is not started, call mg_ota_begin()")); #define MG_SPI_FLASH_READ 0x03 // Read data
} else if (s_addr + MG_ROUND_UP(len, flash->align) > #define MG_SPI_FLASH_PP 0x02 // Page program
(char *) flash->start + flash->size) { #define MG_SPI_FLASH_SE 0x20 // 4k sector erase
MG_ERROR(("Flash overflow: attempting to write past the flash boundary")); #define MG_SPI_FLASH_RDID 0x9f // Read JEDEC ID: vendor, type, log2(size)
} else { #define MG_SPI_FLASH_RDP 0xab // Release from deep power-down
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align); #define MG_SPI_FLASH_SECSZ 4096
if (len_aligned_down) ok = flash->write_fn(s_addr, buf, len_aligned_down); #define MG_SPI_FLASH_PAGESZ 256
if (len_aligned_down < len) {
size_t left = len - len_aligned_down; // Sends cmd, then a 3-byte address if use_addr is true, then writes wbuf or
char tmp[flash->align]; // reads into rbuf, len bytes, in one chip-select cycle
memset(tmp, 0xff, sizeof(tmp)); static void spi_flash_cmd(struct mg_spi *spi, uint8_t cmd, bool use_addr,
memcpy(tmp, (char *) buf + len_aligned_down, left); size_t addr, const void *wbuf, void *rbuf,
ok = flash->write_fn(s_addr + len_aligned_down, tmp, sizeof(tmp)); size_t len) {
} uint8_t hdr[4] = {cmd, (uint8_t) (addr >> 16), (uint8_t) (addr >> 8),
s_crc32 = mg_crc32(s_crc32, (char *) buf, len); // Update CRC (uint8_t) addr};
MG_DEBUG(("%#x %p %lu -> %d", s_addr - len, buf, len, ok)); spi->begin(spi->spi);
s_addr += len; spi->txn(spi->spi, hdr, NULL, use_addr ? 4 : 1);
} if (len > 0) spi->txn(spi->spi, (uint8_t *) wbuf, (uint8_t *) rbuf, len);
return ok; spi->end(spi->spi);
} }
bool mg_ota_flash_end(struct mg_flash *flash) { // Returns the status register
char *base = (char *) flash->start + flash->size / 2; static uint8_t spi_flash_status(struct mg_spi *spi) {
bool ok = false; uint8_t sr = 0xff;
int prev_state = MG_OTA_STATE_GET(); spi_flash_cmd(spi, MG_SPI_FLASH_RDSR, false, 0, NULL, &sr, 1);
if (s_size) { return sr;
size_t size = (size_t) (s_addr - base);
uint32_t crc32 = mg_crc32(0, base, s_size);
if (size == s_size && crc32 == s_crc32) ok = true;
MG_DEBUG(("CRC: %x/%x, size: %lu/%lu, status: %s", s_crc32, crc32, s_size,
size, ok ? "ok" : "fail"));
#ifdef MG_OTA_PUBLIC_KEY
if (ok) {
bool signed_fw = s_size > 68 &&
memcmp((uint8_t *) base + s_size - 4, "MGSG", 4) == 0;
if (signed_fw) {
static const uint8_t s_pubkey[] = MG_OTA_PUBLIC_KEY;
uint8_t hash[32];
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
mg_sha256(hash, (uint8_t *) base, fw_size);
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
(uint8_t *) base + fw_size) == 1;
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
} else {
ok = false;
MG_ERROR(("Unsigned firmware rejected"));
}
}
#endif
s_size = 0;
if (ok) MG_OTA_STATE_SET(MG_OTA_TESTING);
if (ok) ok = flash->swap_fn();
if (!ok) MG_OTA_STATE_SET(prev_state); // undo state in case of failure
}
MG_INFO(("Finishing OTA: %s", ok ? "ok" : "fail"));
return ok;
} }
#endif // Waits up to ms milliseconds for a program or erase to finish
static bool spi_flash_wait(struct mg_spi *spi, uint64_t ms) {
uint64_t deadline = mg_millis() + ms + 1;
while (spi_flash_status(spi) & 1) {
if (mg_millis() > deadline) {
MG_ERROR(("SPI flash timeout"));
return false;
}
}
return true;
}
// Sends write enable. Returns false if the chip did not set WEL, e.g. there
// is no chip, or it ignores writes
static bool spi_flash_wren(struct mg_spi *spi) {
spi_flash_cmd(spi, MG_SPI_FLASH_WREN, false, 0, NULL, NULL, 0);
if ((spi_flash_status(spi) & 3) == 2) return true;
MG_ERROR(("SPI flash write enable failed"));
return false;
}
// Wakes the chip and reads its ID. Sets size from the ID, max 16 MB
bool mg_spi_flash_init(struct mg_spi *spi, size_t *size) {
uint8_t id[3] = {0, 0, 0};
uint64_t t;
spi_flash_cmd(spi, MG_SPI_FLASH_RDP, false, 0, NULL, NULL, 0);
t = mg_millis() + 2; // Wake-up takes microseconds
while (mg_millis() < t) (void) 0;
spi_flash_cmd(spi, MG_SPI_FLASH_RDID, false, 0, NULL, id, sizeof(id));
if (id[0] == 0 || id[0] == 0xff || id[2] < 0x10 || id[2] > 0x20) {
MG_ERROR(("No SPI flash, ID %02x %02x %02x", id[0], id[1], id[2]));
return false;
}
if (id[2] > 0x18) {
MG_INFO(("SPI flash: 3-byte addresses, using the first 16 MB"));
id[2] = 0x18;
}
*size = (size_t) 1 << id[2];
MG_INFO(("SPI flash %02x %02x, %lu bytes", id[0], id[1],
(unsigned long) *size));
return true;
}
// Reads len bytes at flash offset ofs into buf
bool mg_spi_flash_read(struct mg_spi *spi, size_t ofs, void *buf, size_t len) {
spi_flash_cmd(spi, MG_SPI_FLASH_READ, true, ofs, NULL, buf, len);
return true;
}
// Writes len bytes at flash offset ofs, a page at most per program command.
// Erases each 4k sector when the write reaches its start
bool mg_spi_flash_write(struct mg_spi *spi, size_t ofs, const void *buf,
size_t len) {
const uint8_t *p = (const uint8_t *) buf;
while (len > 0) {
size_t n = MG_SPI_FLASH_PAGESZ - ofs % MG_SPI_FLASH_PAGESZ;
if (n > len) n = len;
if (ofs % MG_SPI_FLASH_SECSZ == 0) {
if (!spi_flash_wren(spi)) return false;
spi_flash_cmd(spi, MG_SPI_FLASH_SE, true, ofs, NULL, NULL, 0);
if (!spi_flash_wait(spi, 1000)) return false; // Chips differ, 20-400 ms
}
if (!spi_flash_wren(spi)) return false;
spi_flash_cmd(spi, MG_SPI_FLASH_PP, true, ofs, p, NULL, n);
if (!spi_flash_wait(spi, 50)) return false; // Usually 3 ms max
ofs += n, p += n, len -= n;
}
return true;
}
+32 -4
View File
@@ -1,19 +1,47 @@
#pragma once #pragma once
#include "arch.h" #include "arch.h"
#include "periph.h"
struct mg_flash { struct mg_flash {
void *start; // Address at which flash starts void *start; // Address at which flash starts
size_t size; // Flash size size_t size; // Flash size
size_t secsz; // Sector size size_t secsz; // Sector size
size_t align; // Write alignment size_t align; // Write alignment
bool (*read_fn)(void *, void *, size_t); // Read function
bool (*write_fn)(void *, const void *, size_t); // Write function bool (*write_fn)(void *, const void *, size_t); // Write function
bool (*swap_fn)(void); // Swap partitions bool (*swap_fn)(void); // Swap partitions
bool (*init_fn)(struct mg_flash *); // Optional, fills runtime attributes
}; };
extern struct mg_flash *mg_flash; extern struct mg_flash *mg_flash;
bool mg_lfs_init(size_t size);
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash); // Reads memory-mapped (XIP) flash. Use as read_fn for such flash
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash); bool mg_flash_read_xip(void *addr, void *buf, size_t len);
bool mg_ota_flash_end(struct mg_flash *flash);
// JEDEC SPI NOR flash: W25Q, P25Q, MX25 and similar, up to 16 MB, 3-byte
// addresses, single-line SPI. MG_SPI_FLASH() wraps these into a struct mg_flash
bool mg_spi_flash_init(struct mg_spi *spi, size_t *size);
bool mg_spi_flash_read(struct mg_spi *spi, size_t ofs, void *buf, size_t len);
bool mg_spi_flash_write(struct mg_spi *spi, size_t ofs, const void *buf,
size_t len);
// Defines struct mg_flash name for a JEDEC SPI NOR chip on the struct mg_spi
// pointer spi. Start is 0, so addresses are flash offsets. The size comes
// from the chip on first use, and stays 0 if there is no chip. For example:
// static struct mg_spi s_spi = {NULL, my_begin, my_end, my_txn};
// MG_SPI_FLASH(my_flash, &s_spi);
// #define MG_LFS_FLASH (&my_flash) // In mongoose_config.h
#define MG_SPI_FLASH(name, spi) \
static bool name##_init(struct mg_flash *f) { \
if (f->size == 0) mg_spi_flash_init(spi, &f->size); \
return f->size > 0; \
} \
static bool name##_read(void *a, void *b, size_t n) { \
return mg_spi_flash_read(spi, (size_t) a, b, n); \
} \
static bool name##_write(void *a, const void *b, size_t n) { \
return mg_spi_flash_write(spi, (size_t) a, b, n); \
} \
struct mg_flash name = {NULL, 0, 4096, 1, name##_read, \
name##_write, NULL, name##_init}
+7 -4
View File
@@ -1152,7 +1152,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) { while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs; const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm); int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false; bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len; size_t old_len = c->recv.len;
if (n < 0) { if (n < 0) {
@@ -1172,8 +1172,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL; c->pfn = NULL;
return; return;
} }
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length hcl = mg_http_get_header(&hm, "Content-Length");
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf); hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
} }
is_http_1_0 = is_http_1_0 =
@@ -1187,7 +1190,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460 mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return; return;
} }
} else if (mg_http_get_header(&hm, "Content-length") == NULL) { } else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or // #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length // Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0; bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
+9 -7
View File
@@ -8,7 +8,8 @@
void mg_l2_init(struct mg_tcpip_if *ifp); void mg_l2_init(struct mg_tcpip_if *ifp);
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms); bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame); uint8_t *src, uint8_t *dst, uint8_t *frame,
uint8_t dscp);
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur); size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto, bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw); struct mg_str *pay, struct mg_str *raw);
@@ -27,7 +28,7 @@ size_t mg_l2_driver_output(struct mg_tcpip_if *ifp, size_t len);
// clang-format off // clang-format off
extern void mg_l2_eth_init(struct mg_tcpip_if *); extern void mg_l2_eth_init(struct mg_tcpip_if *);
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool); extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *); extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *); extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *); extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *); extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
@@ -40,7 +41,7 @@ extern uint8_t mg_l2_eth_ip6put(struct mg_l2addr *, uint8_t *);
extern void mg_l2_ppp_init(struct mg_tcpip_if *); extern void mg_l2_ppp_init(struct mg_tcpip_if *);
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool); extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *); extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *); extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *); extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *); extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
@@ -52,13 +53,13 @@ extern uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *, uint8_t *);
#endif #endif
extern void mg_l2_pppoe_init(struct mg_tcpip_if *); extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool); extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *); extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *); extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *); extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
typedef void (*l2_init_fn)(struct mg_tcpip_if *); typedef void (*l2_init_fn)(struct mg_tcpip_if *);
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool); typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *)); typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t));
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *); typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *); typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *)); typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
@@ -101,9 +102,10 @@ bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
} }
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame) { uint8_t *src, uint8_t *dst, uint8_t *frame,
uint8_t dscp) {
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src, return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
(struct mg_l2addr *) dst, frame); (struct mg_l2addr *) dst, frame, dscp);
} }
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) { size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
+3 -2
View File
@@ -63,7 +63,7 @@ bool mg_l2_eth_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst, struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) { uint8_t *frame, uint8_t dscp) {
struct eth_data *d = &ifp->l2data.eth; struct eth_data *d = &ifp->l2data.eth;
struct eth *eth = (struct eth *) frame; struct eth *eth = (struct eth *) frame;
uint8_t *hlp; uint8_t *hlp;
@@ -75,7 +75,8 @@ uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
} else { // Add 802.1Q tag } else { // Add 802.1Q tag
struct qtag *qtag = (struct qtag *) &eth->type; struct qtag *qtag = (struct qtag *) &eth->type;
qtag->tpid = mg_htons(0x8100); qtag->tpid = mg_htons(0x8100);
qtag->tci = mg_htons(d->vlan_id); // PCP = default (best-effort) qtag->tci = mg_htons((uint16_t) ((d->vlan_id & 0x1fff) | // VID + DEI
(dscp >> 3) << 13)); // RFC-9435 5.1.1
hlp += sizeof(*qtag); hlp += sizeof(*qtag);
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]); MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
} }
+11 -8
View File
@@ -139,22 +139,24 @@ static uint8_t *l2_ppp_header(enum mg_l2proto proto, uint8_t *p) {
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst, struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) { uint8_t *frame, uint8_t dscp) {
(void) ifp; (void) ifp;
(void) src; (void) src;
(void) dst; (void) dst;
(void) dscp;
return l2_ppp_header(proto, hdlc_header(frame)); return l2_ppp_header(proto, hdlc_header(frame));
} }
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst, struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame); uint8_t *frame, uint8_t dscp);
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t code, uint16_t id, struct mg_l2addr *src, uint8_t code, uint16_t id, struct mg_l2addr *src,
struct mg_l2addr *dst, uint8_t *frame) { struct mg_l2addr *dst, uint8_t *frame,
uint8_t dscp) {
struct pppoe *pppoe = struct pppoe *pppoe =
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame); (struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame, dscp);
pppoe->vertype = 0x11; pppoe->vertype = 0x11;
pppoe->code = code; pppoe->code = code;
pppoe->id = id; pppoe->id = id;
@@ -163,10 +165,10 @@ static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst, struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) { uint8_t *frame, uint8_t dscp) {
(void) dst; (void) dst;
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0, return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
s_id, src, dst, frame)); s_id, src, dst, frame, dscp));
} }
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) { size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
@@ -202,7 +204,8 @@ static uint8_t *ppp_tx_frame_header(struct mg_tcpip_if *ifp, uint16_t proto) {
return ppp_header(proto, hdlc_header(l2p)); return ppp_header(proto, hdlc_header(l2p));
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0, return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
s_id, (struct mg_l2addr *) ifp->mac, s_id, (struct mg_l2addr *) ifp->mac,
(struct mg_l2addr *) ifp->gwmac, l2p)); (struct mg_l2addr *) ifp->gwmac, l2p,
0));
} }
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len, static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
uint8_t *cur) { uint8_t *cur) {
@@ -517,7 +520,7 @@ static size_t pppoe_tx_frame(struct mg_tcpip_if *ifp, uint8_t code, uint16_t id,
struct mg_l2addr *dst) { struct mg_l2addr *dst) {
uint8_t *l2p = (uint8_t *) ifp->tx.buf; uint8_t *l2p = (uint8_t *) ifp->tx.buf;
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id, uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
(struct mg_l2addr *) ifp->mac, dst, l2p); (struct mg_l2addr *) ifp->mac, dst, l2p, 0);
memmove(p, data, datasz); memmove(p, data, datasz);
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz)); return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
} }
+32 -26
View File
@@ -23,6 +23,10 @@
#define MG_LFS_FD_BASE 12000 // Starting file descriptor number #define MG_LFS_FD_BASE 12000 // Starting file descriptor number
#endif #endif
#ifndef MG_LFS_FLASH
#define MG_LFS_FLASH mg_flash // Flash used by the FS, struct mg_flash *
#endif
#if LFS_USE_RAM && !defined(MG_LFS_BLOCK_SIZE) #if LFS_USE_RAM && !defined(MG_LFS_BLOCK_SIZE)
#define MG_LFS_BLOCK_SIZE 8192 #define MG_LFS_BLOCK_SIZE 8192
#endif #endif
@@ -62,25 +66,27 @@ static struct lfs_config s_cfg = {
.lookahead_size = MG_LFS_BUF_SIZE / 8, .lookahead_size = MG_LFS_BUF_SIZE / 8,
}; };
static bool flash_write_buf(void *addr, const void *buf, size_t len) {
#if LFS_USE_RAM
memmove(addr, buf, len);
return true;
#else
return mg_flash != NULL && mg_flash->write_fn(addr, buf, len);
#endif
}
static int lfs_driver_read(const struct lfs_config *cfg, lfs_block_t block, static int lfs_driver_read(const struct lfs_config *cfg, lfs_block_t block,
lfs_off_t off, void *buf, lfs_size_t len) { lfs_off_t off, void *buf, lfs_size_t len) {
memmove(buf, &s_fs[block * cfg->block_size + off], len); uint8_t *src = &s_fs[block * cfg->block_size + off];
#if LFS_USE_RAM
memmove(buf, src, len);
#else
struct mg_flash *f = MG_LFS_FLASH; // Variable: &x != NULL is a warning
if (f == NULL || !f->read_fn(src, buf, len)) return LFS_ERR_IO;
#endif
return 0; return 0;
} }
static int lfs_driver_prog(const struct lfs_config *cfg, lfs_block_t block, static int lfs_driver_prog(const struct lfs_config *cfg, lfs_block_t block,
lfs_off_t off, const void *buf, lfs_size_t len) { lfs_off_t off, const void *buf, lfs_size_t len) {
uint8_t *dst = &s_fs[block * cfg->block_size + off]; uint8_t *dst = &s_fs[block * cfg->block_size + off];
if (!flash_write_buf(dst, buf, len)) return LFS_ERR_IO; #if LFS_USE_RAM
memmove(dst, buf, len);
#else
struct mg_flash *f = MG_LFS_FLASH;
if (f == NULL || !f->write_fn(dst, buf, len)) return LFS_ERR_IO;
#endif
return 0; return 0;
} }
@@ -106,24 +112,24 @@ bool mg_lfs_init(size_t size) {
if (s_ram_fs == NULL) s_ram_fs = (uint8_t *) mg_calloc(1, size); if (s_ram_fs == NULL) s_ram_fs = (uint8_t *) mg_calloc(1, size);
s_fs = s_ram_fs; s_fs = s_ram_fs;
#else #else
{ struct mg_flash *f = MG_LFS_FLASH;
// Some flash attributes may be initialised on ota_begin if (f == NULL) {
int lev = mg_log_level; MG_ERROR(("no flash"));
mg_log_level = MG_LL_NONE; } else if (f->init_fn != NULL && !f->init_fn(f)) {
mg_ota_begin(1); MG_ERROR(("flash init failed"));
mg_ota_end(); } else if (f->secsz == 0 || f->size < size) {
mg_log_set(lev); MG_ERROR(("secsz %zu flash_size %zu < size %zu", f->secsz, f->size,
} size));
if (mg_flash == NULL || mg_flash->secsz == 0 || mg_flash->size < size) { } else if (f->read_fn == NULL || f->write_fn == NULL) {
MG_ERROR(("mg_flash %p secsz %zu flash_size %zu < size %zu", mg_flash, mg_flash->secsz, mg_flash->size, size)); MG_ERROR(("flash read_fn or write_fn == NULL"));
} else if (mg_flash->write_fn == NULL) {
MG_ERROR(("flash write_fn == NULL"));
} else { } else {
s_cfg.block_size = mg_flash->secsz; s_cfg.block_size = f->secsz;
s_fs = (uint8_t *) mg_flash->start + mg_flash->size - size; s_fs = (uint8_t *) f->start + f->size - size;
} }
#endif #endif
if (s_fs == NULL || s_cfg.block_size == 0 || size % s_cfg.block_size != 0) { // s_fs can be NULL for a flash that starts at 0, e.g. an SPI flash
if ((LFS_USE_RAM && s_fs == NULL) || s_cfg.block_size == 0 ||
size % s_cfg.block_size != 0) {
MG_ERROR(("s_fs %p blk_size %zu size %zu", s_fs, s_cfg.block_size)); MG_ERROR(("s_fs %p blk_size %zu size %zu", s_fs, s_cfg.block_size));
} else { } else {
s_cfg.block_count = (lfs_size_t) (size / s_cfg.block_size); s_cfg.block_count = (lfs_size_t) (size / s_cfg.block_size);
+2
View File
@@ -17,4 +17,6 @@ struct dirent {
}; };
typedef struct mg_lfs_fd DIR; typedef struct mg_lfs_fd DIR;
bool mg_lfs_init(size_t size);
#endif #endif
+1 -1
View File
@@ -8,7 +8,7 @@
#include "ota.h" #include "ota.h"
#include "printf.h" #include "printf.h"
#include "rpc.h" #include "rpc.h"
#include "sntp.h" #include "timer.h"
#include "tls.h" #include "tls.h"
#include "url.h" #include "url.h"
#include "ws.h" #include "ws.h"
+3 -4
View File
@@ -440,9 +440,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1]; m->ack = p[1];
p += 2; p += 2;
if (version == 5) { if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp); len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED; if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp; m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf); m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size; p += len_len + m->props_size;
@@ -473,9 +472,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2; p += 2;
} }
if (p > end) return MQTT_MALFORMED; if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) { if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp); len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED; if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp; m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf); m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size; p += len_len + m->props_size;
+7 -6
View File
@@ -227,7 +227,7 @@ struct pkt {
// L2 API // L2 API
void mg_l2_init(struct mg_tcpip_if *ifp); void mg_l2_init(struct mg_tcpip_if *ifp);
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto, uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame); uint8_t *src, uint8_t *dst, uint8_t *frame, uint8_t dscp);
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur); size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto, bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw); struct mg_str *pay, struct mg_str *raw);
@@ -407,7 +407,7 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
uint8_t *l2p = (uint8_t *) ifp->tx.buf; uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct arp *arp = (struct arp *) mg_l2_header( struct arp *arp = (struct arp *) mg_l2_header(
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac, ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p); mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p, 0);
memset(arp, 0, sizeof(*arp)); memset(arp, 0, sizeof(*arp));
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6, arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
arp->plen = 4; arp->plen = 4;
@@ -454,7 +454,7 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *l2_dst, uint8_t proto,
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt // ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
uint8_t *l2p = (uint8_t *) ifp->tx.buf; uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4, struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
ifp->mac, l2_dst, l2p); ifp->mac, l2_dst, l2p, dscp);
memset(ip, 0, sizeof(*ip)); memset(ip, 0, sizeof(*ip));
ip->ver = 0x45; // Version 4, header length 5 words ip->ver = 0x45; // Version 4, header length 5 words
ip->tos = (uint8_t) (dscp << 2); ip->tos = (uint8_t) (dscp << 2);
@@ -630,8 +630,9 @@ static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// ARP request. Make a response, then send // ARP request. Make a response, then send
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa)); // MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
uint8_t *l2p = (uint8_t *) ifp->tx.buf; uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct arp *arp = (struct arp *) mg_l2_header( struct arp *arp =
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac, mg_l2_getaddr(ifp, pkt->l2), l2p); (struct arp *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
mg_l2_getaddr(ifp, pkt->l2), l2p, 0);
*arp = *pkt->arp; *arp = *pkt->arp;
arp->op = mg_htons(2); arp->op = mg_htons(2);
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha)); memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
@@ -829,7 +830,7 @@ static struct ip6 *tx_ip6(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt // ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
uint8_t *l2p = (uint8_t *) ifp->tx.buf; uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6, struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
ifp->mac, l2_dst, l2p); ifp->mac, l2_dst, l2p, dscp);
memset(ip6, 0, sizeof(*ip6)); memset(ip6, 0, sizeof(*ip6));
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
ip6->label[0] = (uint8_t) (dscp << 6); ip6->label[0] = (uint8_t) (dscp << 6);
-2
View File
@@ -40,8 +40,6 @@ enum {
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connection failed driver and chip specific MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connection failed driver and chip specific
MG_TCPIP_EV_DRIVER, // Driver-defined event driver specific MG_TCPIP_EV_DRIVER, // Driver-defined event driver specific
MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6) MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6)
MG_TCPIP_EV_PHY_RX, // Radio frame received struct mg_str *
MG_TCPIP_EV_PHY_TX, // Radio frame about to be sent struct mg_str *
MG_TCPIP_EV_PHY_INIT, // PHY initialised struct mg_phy * MG_TCPIP_EV_PHY_INIT, // PHY initialised struct mg_phy *
MG_TCPIP_EV_USER // Starting ID for user-defined events MG_TCPIP_EV_USER // Starting ID for user-defined events
}; };
+97
View File
@@ -1,4 +1,5 @@
#include "ota.h" #include "ota.h"
#include "flash.h"
#include "http.h" #include "http.h"
#include "json.h" #include "json.h"
#include "log.h" #include "log.h"
@@ -6,6 +7,15 @@
#include "tls.h" #include "tls.h"
#include "url.h" #include "url.h"
#include "util.h" #include "util.h"
#ifdef MG_OTA_PUBLIC_KEY
#include "sha256.h"
#include "tls_p256.h"
#endif
// In strict C mode, e.g. -std=c99, stdlib.h may not declare alloca()
#if !defined(alloca) && defined(__GNUC__)
#define alloca(size) __builtin_alloca(size)
#endif
#ifndef MG_OTA_MAX_URL_LEN #ifndef MG_OTA_MAX_URL_LEN
#define MG_OTA_MAX_URL_LEN 256 #define MG_OTA_MAX_URL_LEN 256
@@ -277,3 +287,90 @@ void mg_ota_poll(struct mg_mgr *mgr) {
mg_ota_url_check(mgr, MG_OTA_URL, MG_OTA_STATUS_FN); mg_ota_url_check(mgr, MG_OTA_URL, MG_OTA_STATUS_FN);
} }
} }
static char *s_flash_addr; // Current address to write to
static size_t s_size; // Firmware size to flash. In-progress indicator
static uint32_t s_crc32; // Firmware checksum
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash) {
bool ok = false;
if (s_size) {
MG_ERROR(("OTA already in progress. Call mg_ota_end()"));
} else {
size_t half = flash->size / 2;
s_crc32 = 0;
s_flash_addr = (char *) flash->start + half;
MG_DEBUG(("FW %lu bytes, max %lu", new_firmware_size, half));
if (new_firmware_size < half) {
ok = true;
s_size = new_firmware_size;
MG_INFO(("Starting OTA, firmware size %lu", s_size));
} else {
MG_ERROR(("Firmware %lu is too big to fit %lu", new_firmware_size, half - flash->align));
}
}
return ok;
}
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) {
bool ok = false;
if (s_size == 0) {
MG_ERROR(("OTA is not started, call mg_ota_begin()"));
} else if (s_flash_addr + MG_ROUND_UP(len, flash->align) >
(char *) flash->start + flash->size) {
MG_ERROR(("Flash overflow: attempting to write past the flash boundary"));
} else {
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align);
if (len_aligned_down) {
ok = flash->write_fn(s_flash_addr, buf, len_aligned_down);
}
if (len_aligned_down < len) {
size_t left = len - len_aligned_down;
char *tmp = (char *) alloca(flash->align);
memset(tmp, 0xff, flash->align);
memcpy(tmp, (char *) buf + len_aligned_down, left);
ok = flash->write_fn(s_flash_addr + len_aligned_down, tmp, flash->align);
}
s_crc32 = mg_crc32(s_crc32, (char *) buf, len); // Update CRC
MG_DEBUG(("%#x %p %lu -> %d", s_flash_addr - len, buf, len, ok));
s_flash_addr += len;
}
return ok;
}
bool mg_ota_flash_end(struct mg_flash *flash) {
char *base = (char *) flash->start + flash->size / 2;
bool ok = false;
int prev_state = MG_OTA_STATE_GET();
if (s_size) {
size_t size = (size_t) (s_flash_addr - base);
uint32_t crc32 = mg_crc32(0, base, s_size);
if (size == s_size && crc32 == s_crc32) ok = true;
MG_DEBUG(("CRC: %x/%x, size: %lu/%lu, status: %s", s_crc32, crc32, s_size,
size, ok ? "ok" : "fail"));
#ifdef MG_OTA_PUBLIC_KEY
if (ok) {
bool signed_fw = s_size > 68 &&
memcmp((uint8_t *) base + s_size - 4, "MGSG", 4) == 0;
if (signed_fw) {
static const uint8_t s_pubkey[] = MG_OTA_PUBLIC_KEY;
uint8_t hash[32];
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
mg_sha256(hash, (uint8_t *) base, fw_size);
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
(uint8_t *) base + fw_size) == 1;
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
} else {
ok = false;
MG_ERROR(("Unsigned firmware rejected"));
}
}
#endif
s_size = 0;
if (ok) MG_OTA_STATE_SET(MG_OTA_TESTING);
if (ok) ok = flash->swap_fn();
if (!ok) MG_OTA_STATE_SET(prev_state); // undo state in case of failure
}
MG_INFO(("Finishing OTA: %s", ok ? "ok" : "fail"));
return ok;
}
+7
View File
@@ -75,6 +75,13 @@ bool mg_ota_write(const void *buf, size_t len);
// Ends the OTA process and commits the written firmware. // Ends the OTA process and commits the written firmware.
bool mg_ota_end(void); bool mg_ota_end(void);
// Same as above, for a given flash. OTA backends use these to implement
// mg_ota_begin(), mg_ota_write() and mg_ota_end()
struct mg_flash;
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
bool mg_ota_flash_end(struct mg_flash *flash);
// Push-based OTA: mg_http_stream_body() callback that flashes the request // Push-based OTA: mg_http_stream_body() callback that flashes the request
// //
// ```c // ```c
+2
View File
@@ -14,8 +14,10 @@ static struct mg_flash s_mg_flash_ch32v307 = {
480 * 1024, // Size, first 320k is 0-wait 480 * 1024, // Size, first 320k is 0-wait
4 * 1024, // Sector size, 4k 4 * 1024, // Sector size, 4k
4, // Align, 32 bit 4, // Align, 32 bit
mg_flash_read_xip,
mg_ch32v307_write, mg_ch32v307_write,
mg_ch32v307_swap, mg_ch32v307_swap,
NULL, // No init
}; };
#define FLASH_BASE 0x40022000 #define FLASH_BASE 0x40022000
+2
View File
@@ -85,8 +85,10 @@ static struct mg_flash s_mg_flash_imxrt = {
4 * 1024 * 1024, // Size, 4mb 4 * 1024 * 1024, // Size, 4mb
MG_IMXRT_SECTOR_SIZE, // Sector size MG_IMXRT_SECTOR_SIZE, // Sector size
MG_IMXRT_PAGE_SIZE, // Align MG_IMXRT_PAGE_SIZE, // Align
mg_flash_read_xip,
mg_imxrt_write, mg_imxrt_write,
mg_imxrt_swap, mg_imxrt_swap,
NULL, // No init
}; };
struct mg_flexspi_lut_seq { struct mg_flexspi_lut_seq {
+15 -7
View File
@@ -13,14 +13,17 @@
static bool mg_mcxn_write(void *, const void *, size_t); static bool mg_mcxn_write(void *, const void *, size_t);
static bool mg_mcxn_swap(void); static bool mg_mcxn_swap(void);
static bool mg_mcxn_init(struct mg_flash *);
static struct mg_flash s_mg_flash_mcxn = { static struct mg_flash s_mg_flash_mcxn = {
(void *) 0, // Start, filled at init (void *) 0, // Start, filled at init
0, // Size, filled at init 0, // Size, filled at init
0, // Sector size, filled at init 0, // Sector size, filled at init
0, // Align, filled at init 0, // Align, filled at init
mg_flash_read_xip,
mg_mcxn_write, mg_mcxn_write,
mg_mcxn_swap, mg_mcxn_swap,
mg_mcxn_init,
}; };
struct mg_flash_config { struct mg_flash_config {
@@ -162,22 +165,27 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
*(volatile unsigned long *) 0xe000ed0c = 0x5fa0004; *(volatile unsigned long *) 0xe000ed0c = 0x5fa0004;
} }
bool mg_ota_begin(size_t new_firmware_size) { // Fills flash start, size, sector size and alignment from the ROM driver
static bool mg_mcxn_init(struct mg_flash *flash) {
uint32_t status; uint32_t status;
struct mg_flash_config config; struct mg_flash_config config;
if ((status = mg_flash_driver->init(&config)) != 0) { if ((status = mg_flash_driver->init(&config)) != 0) {
MG_ERROR(("Flash driver init error: %lu", status)); MG_ERROR(("Flash driver init error: %lu", status));
return false; return false;
} }
s_mg_flash_mcxn.start = (void *) config.addr; flash->start = (void *) config.addr;
s_mg_flash_mcxn.size = config.size; flash->size = config.size;
s_mg_flash_mcxn.secsz = config.sector_size; flash->secsz = config.sector_size;
s_mg_flash_mcxn.align = config.page_size; flash->align = config.page_size;
mg_flash_driver->deinit(&config); mg_flash_driver->deinit(&config);
MG_DEBUG( MG_DEBUG(
("%lu-byte flash @%p, using %lu-byte sectors with %lu-byte-aligned pages", ("%lu-byte flash @%p, using %lu-byte sectors with %lu-byte-aligned pages",
s_mg_flash_mcxn.size, s_mg_flash_mcxn.start, s_mg_flash_mcxn.secsz, flash->size, flash->start, flash->secsz, flash->align));
s_mg_flash_mcxn.align)); return true;
}
bool mg_ota_begin(size_t new_firmware_size) {
if (!mg_mcxn_init(&s_mg_flash_mcxn)) return false;
return mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxn); return mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxn);
} }
+13 -6
View File
@@ -37,8 +37,10 @@ static bool s_mcxw71_failed;
static bool mg_mcxw71_write(void *, const void *, size_t); static bool mg_mcxw71_write(void *, const void *, size_t);
static bool mg_mcxw71_swap(void); static bool mg_mcxw71_swap(void);
static bool mg_mcxw71_init(struct mg_flash *);
static struct mg_flash s_mg_flash_mcxw71 = { static struct mg_flash s_mg_flash_mcxw71 = {
(void *) 0, 0x100000, 8192, 16, mg_mcxw71_write, mg_mcxw71_swap}; (void *) 0, 0x100000, 8192, 16,
mg_flash_read_xip, mg_mcxw71_write, mg_mcxw71_swap, mg_mcxw71_init};
struct mg_flash *mg_flash = &s_mg_flash_mcxw71; struct mg_flash *mg_flash = &s_mg_flash_mcxw71;
// Everything called while flash is busy, or after the application has been // Everything called while flash is busy, or after the application has been
@@ -110,17 +112,22 @@ MG_IRAM static void mg_mcxw71_apply(size_t size) {
for (;;) __NOP(); for (;;) __NOP();
} }
bool mg_ota_begin(size_t new_firmware_size) { // Initialises the ROM flash API and fills the flash size
uint32_t status; static bool mg_mcxw71_init(struct mg_flash *flash) {
if (s_mcxw71_size != 0 || new_firmware_size == 0) return false; uint32_t status = MG_MCXW71_FLASH_API->init(&s_mcxw71_config);
status = MG_MCXW71_FLASH_API->init(&s_mcxw71_config);
if (status != 0 || s_mcxw71_config.mem[0].base != 0 || if (status != 0 || s_mcxw71_config.mem[0].base != 0 ||
s_mcxw71_config.mem[0].size < 16384 || s_mcxw71_config.mem[0].size < 16384 ||
(s_mcxw71_config.mem[0].size % 16384) != 0) { (s_mcxw71_config.mem[0].size % 16384) != 0) {
MG_ERROR(("Flash init failed: %lu", (unsigned long) status)); MG_ERROR(("Flash init failed: %lu", (unsigned long) status));
return false; return false;
} }
s_mg_flash_mcxw71.size = s_mcxw71_config.mem[0].size; flash->size = s_mcxw71_config.mem[0].size;
return true;
}
bool mg_ota_begin(size_t new_firmware_size) {
if (s_mcxw71_size != 0 || new_firmware_size == 0) return false;
if (!mg_mcxw71_init(&s_mg_flash_mcxw71)) return false;
if (!mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxw71)) return false; if (!mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxw71)) return false;
s_mcxw71_size = new_firmware_size; s_mcxw71_size = new_firmware_size;
s_mcxw71_received = s_mcxw71_pending = 0; s_mcxw71_received = s_mcxw71_pending = 0;
+4 -1
View File
@@ -22,7 +22,10 @@ static struct mg_flash s_mg_flash_picosdk = {
#endif #endif
FLASH_SECTOR_SIZE, // Sector size, from hardware_flash FLASH_SECTOR_SIZE, // Sector size, from hardware_flash
FLASH_PAGE_SIZE, // Align, from hardware_flash FLASH_PAGE_SIZE, // Align, from hardware_flash
mg_picosdk_write, mg_picosdk_swap, mg_flash_read_xip,
mg_picosdk_write,
mg_picosdk_swap,
NULL, // No init
}; };
#define MG_MODULO2(x, m) ((x) & ((m) -1)) #define MG_MODULO2(x, m) ((x) & ((m) -1))
+3 -1
View File
@@ -12,8 +12,10 @@ static struct mg_flash s_mg_flash_frdm = {(void *) 0x08000000, // Start,
0x200000, // Size 0x200000, // Size
0x1000, // Sector size 0x1000, // Sector size
0x100, // Align 0x100, // Align
mg_flash_read_xip,
mg_frdm_write, mg_frdm_write,
mg_frdm_swap}; mg_frdm_swap,
NULL}; // No init
struct mg_flexspi_lut_seq { struct mg_flexspi_lut_seq {
uint8_t seqNum; uint8_t seqNum;
+11 -2
View File
@@ -7,14 +7,17 @@
static bool mg_stm32f_write(void *, const void *, size_t); static bool mg_stm32f_write(void *, const void *, size_t);
static bool mg_stm32f_swap(void); static bool mg_stm32f_swap(void);
static bool mg_stm32f_init(struct mg_flash *);
static struct mg_flash s_mg_flash_stm32f = { static struct mg_flash s_mg_flash_stm32f = {
(void *) 0x08000000, // Start (void *) 0x08000000, // Start
0, // Size, FLASH_SIZE_REG 0, // Size, FLASH_SIZE_REG, set by init
0, // Irregular sector size 0, // Irregular sector size
32, // Align, 256 bit 32, // Align, 256 bit
mg_flash_read_xip,
mg_stm32f_write, mg_stm32f_write,
mg_stm32f_swap, mg_stm32f_swap,
mg_stm32f_init,
}; };
#define MG_FLASH_BASE 0x40023c00 #define MG_FLASH_BASE 0x40023c00
@@ -224,9 +227,15 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
*(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset() *(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset()
} }
// Fills the flash size, read from the chip
static bool mg_stm32f_init(struct mg_flash *flash) {
flash->size = flash_size();
return true;
}
bool mg_ota_begin(size_t new_firmware_size) { bool mg_ota_begin(size_t new_firmware_size) {
size_t max; size_t max;
s_mg_flash_stm32f.size = flash_size(); mg_stm32f_init(&s_mg_flash_stm32f);
#ifdef __ZEPHYR__ #ifdef __ZEPHYR__
*((uint32_t *)0xE000ED94) = 0; *((uint32_t *)0xE000ED94) = 0;
MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed")); MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed"));
+11 -2
View File
@@ -7,14 +7,17 @@
static bool mg_stm32h5_write(void *, const void *, size_t); static bool mg_stm32h5_write(void *, const void *, size_t);
static bool mg_stm32h5_swap(void); static bool mg_stm32h5_swap(void);
static bool mg_stm32h5_init(struct mg_flash *);
static struct mg_flash s_mg_flash_stm32h5 = { static struct mg_flash s_mg_flash_stm32h5 = {
(void *) 0x08000000, // Start (void *) 0x08000000, // Start
0, // Size, FLASHSIZE_BASE 0, // Size, FLASHSIZE_BASE, set by init
8 * 1024, // Sector size, 8k 8 * 1024, // Sector size, 8k
16, // Align, 128 bit 16, // Align, 128 bit
mg_flash_read_xip,
mg_stm32h5_write, mg_stm32h5_write,
mg_stm32h5_swap, mg_stm32h5_swap,
mg_stm32h5_init,
}; };
#define MG_FLASH_BASE 0x40022000 // Base address of the flash controller #define MG_FLASH_BASE 0x40022000 // Base address of the flash controller
@@ -147,8 +150,14 @@ static bool mg_stm32h5_write(void *addr, const void *buf, size_t len) {
return ok; return ok;
} }
// Fills the flash size, read from the chip
static bool mg_stm32h5_init(struct mg_flash *flash) {
flash->size = flash_size();
return true;
}
bool mg_ota_begin(size_t new_firmware_size) { bool mg_ota_begin(size_t new_firmware_size) {
s_mg_flash_stm32h5.size = flash_size(); mg_stm32h5_init(&s_mg_flash_stm32h5);
#ifdef __ZEPHYR__ #ifdef __ZEPHYR__
*((uint32_t *)0xE000ED94) = 0; *((uint32_t *)0xE000ED94) = 0;
MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed")); MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed"));
+11 -2
View File
@@ -14,14 +14,17 @@
static bool mg_stm32h7_write(void *, const void *, size_t); static bool mg_stm32h7_write(void *, const void *, size_t);
static bool mg_stm32h7_swap(void); static bool mg_stm32h7_swap(void);
static bool mg_stm32h7_init(struct mg_flash *);
static struct mg_flash s_mg_flash_stm32h7 = { static struct mg_flash s_mg_flash_stm32h7 = {
(void *) 0x08000000, // Start (void *) 0x08000000, // Start
0, // Size, FLASH_SIZE_REG 0, // Size, FLASH_SIZE_REG, set by init
128 * 1024, // Sector size, 128k 128 * 1024, // Sector size, 128k
32, // Align, 256 bit 32, // Align, 256 bit
mg_flash_read_xip,
mg_stm32h7_write, mg_stm32h7_write,
mg_stm32h7_swap, mg_stm32h7_swap,
mg_stm32h7_init,
}; };
#define FLASH_BASE1 0x52002000 // Base address for bank1 #define FLASH_BASE1 0x52002000 // Base address for bank1
@@ -207,8 +210,14 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
*(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset() *(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset()
} }
// Fills the flash size, read from the chip
static bool mg_stm32h7_init(struct mg_flash *flash) {
flash->size = flash_size();
return true;
}
bool mg_ota_begin(size_t new_firmware_size) { bool mg_ota_begin(size_t new_firmware_size) {
s_mg_flash_stm32h7.size = flash_size(); mg_stm32h7_init(&s_mg_flash_stm32h7);
#ifdef __ZEPHYR__ #ifdef __ZEPHYR__
*((uint32_t *) 0xE000ED94) = 0; *((uint32_t *) 0xE000ED94) = 0;
MG_DEBUG(("Jailbreak %s", *((uint32_t *) 0xE000ED94) == 0 ? "ok" : "failed")); MG_DEBUG(("Jailbreak %s", *((uint32_t *) 0xE000ED94) == 0 ? "ok" : "failed"));
+4 -2
View File
@@ -2,8 +2,10 @@
#include "arch.h" #include "arch.h"
// SPI bus abstraction for SPI-attached chips, e.g. W5500. // SPI bus abstraction for SPI-attached chips, e.g. W5500 or SPI flash, see
// Network drivers get it via the driver_data field // mg_spi_flash_init(). Network drivers get it via the driver_data field.
// Callbacks get the spi field. txn() with NULL write sends filler bytes, with
// NULL read discards what it receives
struct mg_spi { struct mg_spi {
void *spi; // Opaque SPI bus handle passed to all callbacks void *spi; // Opaque SPI bus handle passed to all callbacks
void (*begin)(void *); // Assert chip-select, start of transaction void (*begin)(void *); // Assert chip-select, start of transaction
+2 -28
View File
@@ -1,17 +1,9 @@
// Time / SNTP functions // SNTP functions
#pragma once #pragma once
#include "net.h" #include "net.h"
#include "timer.h"
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync; // Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
// updated automatically by mg_sntp_connect() on each successful response. // updated automatically by mg_sntp_connect() on each successful response.
@@ -21,24 +13,6 @@ extern uint64_t mg_boot_timestamp_ms;
// Until a successful SNTP request completes, this is identical to mg_millis(). // Until a successful SNTP request completes, this is identical to mg_millis().
uint64_t mg_now(void); uint64_t mg_now(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
// Connect to an SNTP server and send a time request. // Connect to an SNTP server and send a time request.
// `url` defaults to "udp://time.google.com:123" when NULL. // `url` defaults to "udp://time.google.com:123" when NULL.
// On success the internal boot timestamp is updated so that mg_now() returns // On success the internal boot timestamp is updated so that mg_now() returns
+61
View File
@@ -2,6 +2,67 @@
#include "timer.h" #include "timer.h"
#include "util.h" #include "util.h"
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
#if MG_ENABLE_CUSTOM_MICROS == 0
uint64_t mg_micros(void) {
#if MG_ARCH == MG_ARCH_ESP32
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() *
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000;
#elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000;
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000;
#elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64();
#elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000;
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
#elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock
#if defined(CLOCK_MONOTONIC_RAW)
// Raw hardware-based time that is not subject to NTP adjustment
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#elif defined(CLOCK_MONOTONIC)
// Affected by the incremental adjustments performed by adjtime and NTP
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
// Affected by discontinuous jumps in the system time and by the incremental
// adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
#elif defined(ARDUINO)
return (uint64_t) micros();
#else
return (uint64_t) time(NULL) * 1000000;
#endif
}
#endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms, void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms,
unsigned flags, void (*fn)(void *), void *arg) { unsigned flags, void (*fn)(void *), void *arg) {
t->period_ms = ms, t->expire = 0; t->period_ms = ms, t->expire = 0;
+27
View File
@@ -2,6 +2,33 @@
#include "arch.h" #include "arch.h"
// Return microseconds since boot. Never goes backwards.
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
uint64_t mg_micros(void);
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
// Deprecated: use mg_micros() instead.
uint64_t mg_millis(void);
// Checks whether a periodic deadline has expired.
//
// Returns:
// True when the timer has fired; false otherwise.
// Example:
// uint64_t timer = 0;
// if (mg_timer_expired(&timer, 1000, mg_millis())) {
// MG_INFO(("every second"));
// }
// Full examples:
// tutorials/mqtt/mqtt-client
// Related APIs:
// mg_millis(), mg_now()
// Notes:
// Initialise *expiration to 0 before first use. On expiry, this function
// advances *expiration by period and handles time wrap-around.
bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
struct mg_timer { struct mg_timer {
uint64_t period_ms; // Timer period in milliseconds uint64_t period_ms; // Timer period in milliseconds
uint64_t expire; // Expiration timestamp in milliseconds uint64_t expire; // Expiration timestamp in milliseconds
+1 -1
View File
@@ -2,6 +2,7 @@
#include "config.h" #include "config.h"
#include "printf.h" #include "printf.h"
#include "sha256.h" #include "sha256.h"
#include "sntp.h"
#include "tls.h" #include "tls.h"
#include "tls_aes128.h" #include "tls_aes128.h"
#include "tls_chacha20.h" #include "tls_chacha20.h"
@@ -316,7 +317,6 @@ static void mg_tls_derive_secret(const char *label, uint8_t *key, size_t keysz,
size_t labelsz = strlen(label); size_t labelsz = strlen(label);
uint8_t secret[32]; uint8_t secret[32];
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz}; uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
// TODO: assert lengths of label, key, data and hash
if (labelsz > 0) memmove(packed + 3, label, labelsz); if (labelsz > 0) memmove(packed + 3, label, labelsz);
packed[3 + labelsz] = (uint8_t) datasz; packed[3 + labelsz] = (uint8_t) datasz;
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz); if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
-61
View File
@@ -197,67 +197,6 @@ bool mg_path_is_sane(const struct mg_str path) {
return true; return true;
} }
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
#if MG_ENABLE_CUSTOM_MICROS == 0
uint64_t mg_micros(void) {
#if MG_ARCH == MG_ARCH_ESP32
return (uint64_t) esp_timer_get_time();
#elif MG_ENABLE_FREERTOS
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
#elif MG_ARCH == MG_ARCH_THREADX
return (uint64_t) tx_time_get() *
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_TIRTOS
return (uint64_t) Clock_getTicks() * 1000;
#elif MG_ARCH == MG_ARCH_ZEPHYR
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
return (uint64_t) rt_time_get() * 1000;
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
1000;
#elif MG_ARCH == MG_ARCH_RTTHREAD
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
#elif MG_ARCH == MG_ARCH_WIN32
return (uint64_t) GetTickCount() * 1000;
#elif MG_ARCH == MG_ARCH_PICOSDK
return time_us_64();
#elif MG_ARCH == MG_ARCH_CUBE
return (uint64_t) HAL_GetTick() * 1000;
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
#elif MG_ARCH == MG_ARCH_UNIX
struct timespec ts = {0, 0};
// See #1615 - prefer monotonic clock
#if defined(CLOCK_MONOTONIC_RAW)
// Raw hardware-based time that is not subject to NTP adjustment
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#elif defined(CLOCK_MONOTONIC)
// Affected by the incremental adjustments performed by adjtime and NTP
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
// Affected by discontinuous jumps in the system time and by the incremental
// adjustments performed by adjtime and NTP
clock_gettime(CLOCK_REALTIME, &ts);
#endif
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
#elif defined(ARDUINO)
return (uint64_t) micros();
#else
return (uint64_t) time(NULL) * 1000000;
#endif
}
#endif
#if MG_ENABLE_CUSTOM_MILLIS == 0
uint64_t mg_millis(void) {
return mg_micros() / 1000;
}
#endif
// network format equates big endian order // network format equates big endian order
uint16_t mg_ntohs(uint16_t net) { uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net); return MG_LOAD_BE16(&net);
+1 -1
View File
@@ -3,8 +3,8 @@
#include "arch.h" #include "arch.h"
#include "config.h" #include "config.h"
#include "net.h" #include "net.h"
#include "sntp.h"
#include "str.h" #include "str.h"
#include "timer.h"
#if MG_ENABLE_ASSERT #if MG_ENABLE_ASSERT
#include <assert.h> #include <assert.h>
+17 -1
View File
@@ -817,6 +817,12 @@ static void test_mqtt_parse(void) {
struct mg_mqtt_prop prop; struct mg_mqtt_prop prop;
// MQTT5 PUBLISH with Subscription Identifier = 5 // MQTT5 PUBLISH with Subscription Identifier = 5
const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05}; const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05};
// MQTT5 PUBLISH with no properties and no payload
const uint8_t pub_empty[] = {0x30, 0x04, 0x00, 0x01, 'x', 0x00};
// MQTT5 PUBLISH with no properties and a one-byte payload
const uint8_t pub_byte[] = {0x30, 0x05, 0x00, 0x01, 'x', 0x00, 'y'};
// MQTT5 PUBLISH without its mandatory Property Length
const uint8_t pub_no_props[] = {0x30, 0x03, 0x00, 0x01, 'x'};
// MQTT5 CONNACK with Server Keep Alive = 60 // MQTT5 CONNACK with Server Keep Alive = 60
const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C}; const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C};
@@ -825,6 +831,13 @@ static void test_mqtt_parse(void) {
ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK); ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK);
mg_mqtt_next_prop(&mm, &prop, 0); mg_mqtt_next_prop(&mm, &prop, 0);
ASSERT(mg_mqtt_parse(pub_empty, sizeof(pub_empty), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 0);
ASSERT(mg_mqtt_parse(pub_byte, sizeof(pub_byte), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 1 && mm.data.buf[0] == 'y');
ASSERT(mg_mqtt_parse(pub_no_props, sizeof(pub_no_props), 5, &mm) ==
MQTT_MALFORMED);
ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK); ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK);
ASSERT(mm.ack == 0); ASSERT(mm.ack == 0);
ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0); ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0);
@@ -1277,6 +1290,9 @@ static void test_http_server(void) {
"POST /body HTTP/1.1\r\n" "POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nkuku") == 200); "Content-Length: 4\r\n\r\nkuku") == 200);
ASSERT(cmpbody(buf, "kuku") == 0); ASSERT(cmpbody(buf, "kuku") == 0);
ASSERT(fetch(&mgr, buf, url, "%s",
"POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nku") == 0);
ASSERT(fetch(&mgr, buf, url, "GET /ssi HTTP/1.1\r\n\r\n") == 301); ASSERT(fetch(&mgr, buf, url, "GET /ssi HTTP/1.1\r\n\r\n") == 301);
ASSERT(fetch(&mgr, buf, url, "GET /ssi/ HTTP/1.1\r\n\r\n") == 200); ASSERT(fetch(&mgr, buf, url, "GET /ssi/ HTTP/1.1\r\n\r\n") == 200);
@@ -3334,7 +3350,7 @@ static void test_http_stream_errors(void) {
ASSERT(state.ends == 1); ASSERT(state.ends == 1);
if (c.is_client) { if (c.is_client) {
ASSERT(c.send.len == 0); ASSERT(c.send.len == 0);
ASSERT(c.is_closing == (cases[i].code != 200)); ASSERT(c.is_closing == (unsigned) (cases[i].code != 200));
} else { } else {
ASSERT(mg_http_parse((char *) c.send.buf, c.send.len, &hm) > 0); ASSERT(mg_http_parse((char *) c.send.buf, c.send.len, &hm) > 0);
ASSERT(mg_http_status(&hm) == cases[i].code); ASSERT(mg_http_status(&hm) == cases[i].code);
+1 -1
View File
@@ -287,7 +287,7 @@ void mg_dash_init(struct mg_mgr *mgr) {
MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph); MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_actions); MG_DASH_ADD_FIELD_SET(&s_dash, &set_actions);
#if MG_ARCH == MG_ARCH_UNIX #if MG_ARCH == MG_ARCH_UNIX || MG_ARCH == MG_ARCH_WIN32 || MG_ENABLE_LFS
// Create demo files in the upload directory // Create demo files in the upload directory
mkdir("/tmp", 0755); mkdir("/tmp", 0755);
mkdir("/tmp/dashboard", 0755); mkdir("/tmp/dashboard", 0755);
+9 -3
View File
@@ -12,7 +12,7 @@ NRFUTIL ?= adafruit-nrfutil
CFLAGS = -W -Wall -Wextra -Werror -Wshadow -Wdouble-promotion CFLAGS = -W -Wall -Wextra -Werror -Wshadow -Wdouble-promotion
CFLAGS += -Wformat-truncation -fno-common -Wno-conversion -Wno-sign-conversion CFLAGS += -Wformat-truncation -fno-common -Wno-conversion -Wno-sign-conversion
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Inrfx/mdk CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Inrfx/mdk -Ilittlefs
CFLAGS += -DNRF52840_XXAA -D__STARTUP_CLEAR_BSS -D__START=main -D__HEAP_SIZE=0 CFLAGS += -DNRF52840_XXAA -D__STARTUP_CLEAR_BSS -D__START=main -D__HEAP_SIZE=0
CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
CFLAGS += $(CFLAGS_EXTRA) CFLAGS += $(CFLAGS_EXTRA)
@@ -22,13 +22,15 @@ LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs=nano.specs -lc -lgcc
LDFLAGS += -Wl,--gc-sections -Wl,-Map=$@.map LDFLAGS += -Wl,--gc-sections -Wl,-Map=$@.map
SOURCES = hal.c main.c $(MGDIR)/mongoose.c SOURCES = hal.c main.c $(MGDIR)/mongoose.c
SOURCES += littlefs/lfs.c littlefs/lfs_util.c
CFLAGS += -Wno-shadow # Disabled for littlefs/lfs.c
SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c
SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c
SOURCES += nrfx/mdk/system_nrf52840.c nrfx/mdk/gcc_startup_nrf52840.S SOURCES += nrfx/mdk/system_nrf52840.c nrfx/mdk/gcc_startup_nrf52840.S
all build: firmware.hex firmware.bin all build: firmware.hex firmware.bin
firmware.elf: cmsis_core nrfx $(SOURCES) $(wildcard *.h) link.ld Makefile firmware.elf: cmsis_core nrfx littlefs $(SOURCES) $(wildcard *.h) link.ld Makefile
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
arm-none-eabi-size --format=berkeley $@ arm-none-eabi-size --format=berkeley $@
@@ -52,10 +54,14 @@ cmsis_core:
nrfx: nrfx:
git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.0 https://github.com/NordicSemiconductor/nrfx $@ git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.0 https://github.com/NordicSemiconductor/nrfx $@
littlefs/lfs.c littlefs/lfs_util.c: littlefs
littlefs:
git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.3 https://github.com/littlefs-project/littlefs $@
$(MGDIR)/tutorials/device-dashboard/full/file_data.c: $(MGDIR)/tutorials/device-dashboard/full/dashboard.html $(MGDIR)/tutorials/device-dashboard/full/file_data.c: $(MGDIR)/tutorials/device-dashboard/full/dashboard.html
cd $(dir $@) && $(MAKE) $(notdir $@) cd $(dir $@) && $(MAKE) $(notdir $@)
clean: clean:
rm -rf firmware.* cmsis_core nrfx rm -rf firmware.* cmsis_core nrfx littlefs
.PHONY: all build flash clean .PHONY: all build flash clean
+7 -7
View File
@@ -40,19 +40,19 @@ void *_sbrk(int incr) {
return prev_heap; return prev_heap;
} }
// Newlib stubs // Newlib stubs. Weak ones are replaced by littlefs, see MG_ENABLE_LFS
struct stat; struct stat;
int _fstat(int fd, struct stat *st) { __attribute__((weak)) int _fstat(int fd, struct stat *st) {
(void) fd, (void) st; (void) fd, (void) st;
return -1; return -1;
} }
int _open(const char *path) { __attribute__((weak)) int _open(const char *path) {
(void) path; (void) path;
return -1; return -1;
} }
int _close(int fd) { __attribute__((weak)) int _close(int fd) {
(void) fd; (void) fd;
return -1; return -1;
} }
@@ -62,7 +62,7 @@ int _isatty(int fd) {
return 1; return 1;
} }
int _lseek(int fd, int ptr, int dir) { __attribute__((weak)) int _lseek(int fd, int ptr, int dir) {
(void) fd, (void) ptr, (void) dir; (void) fd, (void) ptr, (void) dir;
return 0; return 0;
} }
@@ -80,12 +80,12 @@ int _getpid(void) {
return -1; return -1;
} }
int _write(int fd, char *ptr, int len) { __attribute__((weak)) int _write(int fd, char *ptr, int len) {
(void) fd, (void) ptr, (void) len; (void) fd, (void) ptr, (void) len;
return -1; return -1;
} }
int _read(int fd, char *ptr, int len) { __attribute__((weak)) int _read(int fd, char *ptr, int len) {
(void) fd, (void) ptr, (void) len; (void) fd, (void) ptr, (void) len;
return -1; return -1;
} }
+61
View File
@@ -15,6 +15,12 @@
#define IMU_PWR PIN(1, 8) // LSM6DS3TR-C IMU power, active high (Sense only) #define IMU_PWR PIN(1, 8) // LSM6DS3TR-C IMU power, active high (Sense only)
#define IMU_SCL PIN(0, 27) // IMU I2C bus #define IMU_SCL PIN(0, 27) // IMU I2C bus
#define IMU_SDA PIN(0, 7) #define IMU_SDA PIN(0, 7)
#define FLASH_CS PIN(0, 25) // P25Q16H 2 MB flash, used over plain SPI
#define FLASH_SCK PIN(0, 21) // The board wires it for QSPI: IO0..IO3
#define FLASH_MOSI PIN(0, 20) // IO0
#define FLASH_MISO PIN(0, 24) // IO1
#define FLASH_WP PIN(0, 22) // IO2, active low
#define FLASH_HOLD PIN(0, 23) // IO3, active low
uint64_t hal_get_tick(void); uint64_t hal_get_tick(void);
size_t hal_ram_used(void); size_t hal_ram_used(void);
@@ -49,6 +55,61 @@ static inline void hal_gpio_output(uint16_t pin) {
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos); (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
} }
// SPIM2 to the on-board flash, 8 MHz, mode 0. SPIM0 is not used because it
// shares its registers with TWIM0, the IMU's I2C
static inline void hal_spi_init(void) {
hal_gpio_write(FLASH_CS, true);
hal_gpio_write(FLASH_WP, true);
hal_gpio_write(FLASH_HOLD, true);
hal_gpio_write(FLASH_SCK, false);
hal_gpio_output(FLASH_CS);
hal_gpio_output(FLASH_WP);
hal_gpio_output(FLASH_HOLD);
hal_gpio_output(FLASH_SCK); // Its input buffer must stay connected
hal_gpio_output(FLASH_MOSI);
hal_gpio_bank(FLASH_MISO)->PIN_CNF[PINNO(FLASH_MISO)] =
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
NRF_SPIM2->PSEL.SCK = FLASH_SCK; // PIN() matches the PSEL port/pin format
NRF_SPIM2->PSEL.MOSI = FLASH_MOSI;
NRF_SPIM2->PSEL.MISO = FLASH_MISO;
NRF_SPIM2->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M8;
NRF_SPIM2->CONFIG = 0; // MSB first, mode 0
NRF_SPIM2->ORC = 0xff; // Sent when there is nothing to write
NRF_SPIM2->ENABLE = SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos;
}
// struct mg_spi callbacks. spi is unused, there is one bus
static inline void hal_spi_begin(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, false);
}
static inline void hal_spi_end(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, true);
}
// Sends write and receives into read, len bytes. Either can be NULL. Buffers
// must be in RAM, for EasyDMA
static inline void hal_spi_txn(void *spi, uint8_t *write, uint8_t *read,
size_t len) {
(void) spi;
while (len > 0) {
size_t n = len > 0xffff ? 0xffff : len; // MAXCNT is 16 bits
NRF_SPIM2->TXD.PTR = (uint32_t) write;
NRF_SPIM2->TXD.MAXCNT = write == NULL ? 0 : n;
NRF_SPIM2->RXD.PTR = (uint32_t) read;
NRF_SPIM2->RXD.MAXCNT = read == NULL ? 0 : n;
NRF_SPIM2->EVENTS_END = 0;
NRF_SPIM2->TASKS_START = 1;
while (!NRF_SPIM2->EVENTS_END) (void) 0;
if (write != NULL) write += n;
if (read != NULL) read += n;
len -= n;
}
}
// Vectors above the bootloader, 64 MHz crystal clock, 1 ms SysTick // Vectors above the bootloader, 64 MHz crystal clock, 1 ms SysTick
static inline void hal_system_init(void) { static inline void hal_system_init(void) {
extern uint32_t __isr_vector[]; extern uint32_t __isr_vector[];
@@ -25,6 +25,13 @@ bool mg_random(void *buf, size_t len) {
return true; return true;
} }
// The on-board P25Q16H 2 MB flash for littlefs, see MG_LFS_FLASH in
// mongoose_config.h. Mongoose's SPI flash API drives it over hal.h's SPI.
// Early boards (V1.1) have the chip not fitted. Then init fails and the
// filesystem is not available
static struct mg_spi s_spi = {NULL, hal_spi_begin, hal_spi_end, hal_spi_txn};
MG_SPI_FLASH(mg_flash_spi, &s_spi);
int main(void) { int main(void) {
hal_system_init(); hal_system_init();
hal_rng_init(); hal_rng_init();
@@ -35,6 +42,7 @@ int main(void) {
hal_gpio_output(LED_GREEN); hal_gpio_output(LED_GREEN);
hal_gpio_output(LED_BLUE); hal_gpio_output(LED_BLUE);
int imu = hal_imu_init(); // For the dashboard's real-time graph int imu = hal_imu_init(); // For the dashboard's real-time graph
hal_spi_init(); // On-board flash, see MG_SPI_FLASH() above
struct mg_mgr mgr; struct mg_mgr mgr;
mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h
@@ -10,6 +10,12 @@
#define MG_ENABLE_USB_ETHERNET 1 // USB NCM Ethernet interface #define MG_ENABLE_USB_ETHERNET 1 // USB NCM Ethernet interface
#define MG_ENABLE_USB_SERIAL 1 // USB serial log console #define MG_ENABLE_USB_SERIAL 1 // USB serial log console
// Filesystem on the on-board 2 MB flash, see main.c
#define MG_ENABLE_LFS 1
#define MG_LFS_SIZE (2 * 1024 * 1024) // The whole chip
#define MG_LFS_FLASH (&mg_flash_spi)
extern struct mg_flash mg_flash_spi;
// Static device address; the driver serves DHCP to the USB host. Remove // Static device address; the driver serves DHCP to the USB host. Remove
// these to get an address over DHCP instead, e.g. from Internet Sharing // these to get an address over DHCP instead, e.g. from Internet Sharing
#define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1) #define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1)
@@ -0,0 +1,3 @@
// Replaces newlib's sys/dirent.h, which is an #error. With MG_ENABLE_LFS,
// struct dirent and DIR come from mongoose.h
#pragma once
@@ -8,31 +8,27 @@
#define LED_LAN PIN(0, 0) // Blue, active low #define LED_LAN PIN(0, 0) // Blue, active low
#define LED_RED PIN(0, 1) // Red, active low #define LED_RED PIN(0, 1) // Red, active low
static uint64_t s_radio_led_until;
// Cumulative totals. Up: radio -> LAN; down: LAN -> radio queue. // Cumulative totals. Up: radio -> LAN; down: LAN -> radio queue.
static uint64_t s_frames_up, s_frames_down, s_bytes_up, s_bytes_down; static uint64_t s_frames_up, s_frames_down, s_bytes_up, s_bytes_down;
static uint64_t s_radio_sent, s_radio_received;
// Radio interface events: count radio frames, blink on reception // Radio activity: successful frames crossing the bridge keep the LED on.
static void radio_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) { static uint64_t s_radio_led_until;
if (ev == MG_TCPIP_EV_PHY_TX) s_radio_sent++;
if (ev == MG_TCPIP_EV_PHY_RX) {
s_radio_received++;
s_radio_led_until = mg_millis() + 2;
}
(void) ifp, (void) ev_data;
}
static size_t eth_up(const void *buf, size_t len, struct mg_tcpip_if *ifp) { static size_t eth_up(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
size_t sent = mg_tcpip_driver_rw612.tx(buf, len, ifp); size_t sent = mg_tcpip_driver_rw612.tx(buf, len, ifp);
if (sent != 0) s_frames_up++, s_bytes_up += sent; if (sent != 0) {
s_frames_up++, s_bytes_up += sent;
s_radio_led_until = mg_millis() + 2;
}
return sent; return sent;
} }
static size_t eth_down(const void *buf, size_t len, struct mg_tcpip_if *ifp) { static size_t eth_down(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
size_t queued = mg_tcpip_driver_rw612w.tx(buf, len, ifp); size_t queued = mg_tcpip_driver_rw612w.tx(buf, len, ifp);
if (queued != 0) s_frames_down++, s_bytes_down += queued; if (queued != 0) {
s_frames_down++, s_bytes_down += queued;
s_radio_led_until = mg_millis() + 2;
}
return queued; return queued;
} }
@@ -66,7 +62,6 @@ int main(void) {
radio_driver = mg_tcpip_driver_rw612w; radio_driver = mg_tcpip_driver_rw612w;
radio_driver.tx = eth_down; radio_driver.tx = eth_down;
radio.driver = &radio_driver; radio.driver = &radio_driver;
radio.fn = radio_fn;
if (!radio.driver->init(&radio)) return 1; if (!radio.driver->init(&radio)) return 1;
eth_data.mdc_cr = MG_DRIVER_MDC_CR; eth_data.mdc_cr = MG_DRIVER_MDC_CR;
@@ -95,9 +90,8 @@ int main(void) {
if (mg_timer_expired(&stats_timer, 5000, now)) { if (mg_timer_expired(&stats_timer, 5000, now)) {
MG_INFO( MG_INFO(
("ETH frames up/down %llu/%llu, bytes up/down " ("ETH frames up/down %llu/%llu, bytes up/down "
"%llu/%llu, radio frames sent/rcvd %llu/%llu", "%llu/%llu",
s_frames_up, s_frames_down, s_bytes_up, s_bytes_down, s_radio_sent, s_frames_up, s_frames_down, s_bytes_up, s_bytes_down));
s_radio_received));
} }
if (eth.nrecv != lan_received) { if (eth.nrecv != lan_received) {
lan_received = eth.nrecv; lan_received = eth.nrecv;
@@ -9,11 +9,14 @@ CFLAGS = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion
CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Icmsis_h7/Include CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Icmsis_h7/Include
CFLAGS += -Ilittlefs
CFLAGS += -mcpu=cortex-m7 -mthumb -mfloat-abi=hard -mfpu=fpv5-d16 -DCORE_CM7 CFLAGS += -mcpu=cortex-m7 -mthumb -mfloat-abi=hard -mfpu=fpv5-d16 -DCORE_CM7
CFLAGS += $(CFLAGS_EXTRA) CFLAGS += $(CFLAGS_EXTRA)
LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections
SOURCES = main.c hal.c $(MGDIR)/mongoose.c SOURCES = main.c hal.c $(MGDIR)/mongoose.c
SOURCES += littlefs/lfs.c littlefs/lfs_util.c
CFLAGS += -Wno-conversion -Wno-shadow # Disabled for littlefs/lfs.c
SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c
SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c
SOURCES += cmsis_h7/Source/Templates/gcc/startup_stm32h747xx.s SOURCES += cmsis_h7/Source/Templates/gcc/startup_stm32h747xx.s
@@ -24,7 +27,7 @@ firmware.bin: firmware.elf
arm-none-eabi-objcopy -O binary $< $@ arm-none-eabi-objcopy -O binary $< $@
arm-none-eabi-size --format=berkeley $< arm-none-eabi-size --format=berkeley $<
firmware.elf: cmsis_core cmsis_h7 $(SOURCES) $(wildcard *.h) $(MGDIR)/mongoose.h link.ld Makefile firmware.elf: cmsis_core cmsis_h7 littlefs $(SOURCES) $(wildcard *.h) $(MGDIR)/mongoose.h link.ld Makefile
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
flash: firmware.bin flash: firmware.bin
@@ -36,8 +39,12 @@ cmsis_core:
cmsis_h7: cmsis_h7:
git clone -q -c advice.detachedHead=false --depth 1 -b v1.10.4 https://github.com/STMicroelectronics/cmsis_device_h7 $@ git clone -q -c advice.detachedHead=false --depth 1 -b v1.10.4 https://github.com/STMicroelectronics/cmsis_device_h7 $@
littlefs/lfs.c littlefs/lfs_util.c: littlefs
littlefs:
git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.3 https://github.com/littlefs-project/littlefs $@
$(MGDIR)/tutorials/device-dashboard/full/file_data.c: $(MGDIR)/tutorials/device-dashboard/full/file_data.c:
cd $(dir $@) && $(MAKE) $(notdir $@) cd $(dir $@) && $(MAKE) $(notdir $@)
clean: clean:
rm -rf firmware.* cmsis_* rm -rf firmware.* cmsis_* littlefs
@@ -13,6 +13,15 @@
#define LED_3 PIN('K', 7) // On-board LED pin (blue) #define LED_3 PIN('K', 7) // On-board LED pin (blue)
#define LED LED_2 // Use green LED for blinking #define LED LED_2 // Use green LED for blinking
// On-board 16 MB QSPI flash, used over plain SPI. Pins from Arduino's mbed-os,
// TARGET_PORTENTA_H7 PinNames.h: QSPI_FLASH1_*
#define FLASH_CS PIN('G', 6)
#define FLASH_SCK PIN('F', 10)
#define FLASH_MOSI PIN('D', 11) // IO0
#define FLASH_MISO PIN('D', 12) // IO1
#define FLASH_WP PIN('F', 7) // IO2, active low
#define FLASH_HOLD PIN('D', 13) // IO3, active low
#include <stm32h747xx.h> #include <stm32h747xx.h>
#include <stdbool.h> #include <stdbool.h>
@@ -103,6 +112,50 @@ static inline void hal_gpio_output(uint16_t pin) {
HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0); HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0);
} }
// Bit-banged SPI to the on-board flash, mode 0. No SPI peripheral reaches the
// flash pins, they are wired for QUADSPI
static inline void hal_spi_init(void) {
uint16_t outs[] = {FLASH_CS, FLASH_WP, FLASH_HOLD, FLASH_SCK, FLASH_MOSI};
for (size_t i = 0; i < sizeof(outs) / sizeof(outs[0]); i++) {
hal_gpio_write(outs[i], outs[i] != FLASH_SCK); // SCK low, the rest high
hal_gpio_output(outs[i]);
}
hal_gpio_init(FLASH_MISO, HAL_GPIO_MODE_INPUT, HAL_GPIO_OTYPE_PUSH_PULL,
HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0);
}
// struct mg_spi callbacks. spi is unused, there is one bus
static inline void hal_spi_begin(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, false);
}
static inline void hal_spi_end(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, true);
}
// Sends write and receives into read, len bytes. Either can be NULL, then
// 0xff is sent, or the input is dropped
static inline void hal_spi_txn(void *spi, uint8_t *write, uint8_t *read,
size_t len) {
GPIO_TypeDef *sck = hal_gpio_bank(FLASH_SCK), *mosi = hal_gpio_bank(FLASH_MOSI),
*miso = hal_gpio_bank(FLASH_MISO);
uint32_t sck_bit = BIT(PINNO(FLASH_SCK)), mosi_bit = BIT(PINNO(FLASH_MOSI)),
miso_bit = BIT(PINNO(FLASH_MISO));
(void) spi;
for (size_t i = 0; i < len; i++) {
uint32_t out = write == NULL ? 0xff : write[i], in = 0;
for (int b = 7; b >= 0; b--) {
mosi->BSRR = mosi_bit << ((out >> b) & 1 ? 0 : 16);
sck->BSRR = sck_bit; // Rising edge: the flash samples MOSI
in = (in << 1) | ((miso->IDR & miso_bit) ? 1 : 0);
sck->BSRR = sck_bit << 16; // Falling edge: the flash shifts out
}
if (read != NULL) read[i] = (uint8_t) in;
}
}
static inline void hal_rng_init(void) { static inline void hal_rng_init(void) {
RCC->D2CCIP2R |= RCC_D2CCIP2R_RNGSEL_0; // RNG clock source pll1_q_ck RCC->D2CCIP2R |= RCC_D2CCIP2R_RNGSEL_0; // RNG clock source pll1_q_ck
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; // Enable RNG clock RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; // Enable RNG clock
@@ -24,11 +24,17 @@ bool mg_random(void *buf, size_t len) {
return true; return true;
} }
// The on-board 16 MB flash for littlefs, see MG_LFS_FLASH in
// mongoose_config.h. Mongoose's SPI flash API drives it over hal.h's SPI
static struct mg_spi s_spi = {NULL, hal_spi_begin, hal_spi_end, hal_spi_txn};
MG_SPI_FLASH(mg_flash_spi, &s_spi);
int main(void) { int main(void) {
hal_rng_init(); hal_rng_init();
hal_gpio_output(LED); hal_gpio_output(LED);
hal_gpio_write(LED, true); // Off, the LEDs are active low hal_gpio_write(LED, true); // Off, the LEDs are active low
hal_usb_init(); hal_usb_init();
hal_spi_init(); // On-board flash, see MG_SPI_FLASH() above
struct mg_mgr mgr; struct mg_mgr mgr;
mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h
@@ -14,6 +14,12 @@
#define MG_USB_DWC2_BASE USB1_OTG_HS_PERIPH_BASE // OTG_HS #define MG_USB_DWC2_BASE USB1_OTG_HS_PERIPH_BASE // OTG_HS
#define MG_USB_DWC2_ULPI 1 // External ULPI PHY #define MG_USB_DWC2_ULPI 1 // External ULPI PHY
// Filesystem on the on-board 16 MB flash, see main.c
#define MG_ENABLE_LFS 1
#define MG_LFS_SIZE (16 * 1024 * 1024) // The whole chip
#define MG_LFS_FLASH (&mg_flash_spi)
extern struct mg_flash mg_flash_spi;
// Static device address; the driver serves DHCP to the USB host. Remove // Static device address; the driver serves DHCP to the USB host. Remove
// these to get an address over DHCP instead, e.g. from Internet Sharing // these to get an address over DHCP instead, e.g. from Internet Sharing
#define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1) #define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1)
@@ -0,0 +1,3 @@
// Replaces newlib's sys/dirent.h, which is an #error. With MG_ENABLE_LFS,
// struct dirent and DIR come from mongoose.h
#pragma once