mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-04 21:58:22 +00:00
Compare commits
12
Commits
spi
..
link-local
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d597b89bd4 | ||
|
|
f3189519d8 | ||
|
|
f03c00a516 | ||
|
|
f02ea9859e | ||
|
|
d7a756a24a | ||
|
|
57e30ab2e9 | ||
|
|
9ac1106e74 | ||
|
|
5c4d06552c | ||
|
|
39bfa22037 | ||
|
|
24f5519463 | ||
|
|
948f16bab3 | ||
|
|
48a6b0d422 |
No files matched your search
+412
-242
File diff suppressed because it is too large.
Load diff
+73
-35
@@ -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,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
|
||||||
|
|
||||||
|
|||||||
+3
-7
@@ -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
@@ -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
@@ -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
@@ -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;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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
@@ -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
@@ -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;
|
||||||
|
|||||||
+36
-31
@@ -380,6 +380,7 @@ static const struct mg_addr ip6_allnodes = {
|
|||||||
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
|
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
|
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
|
||||||
|
#define MG_IP6UNSPEC(a) (a[0] == 0 && a[1] == 0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void settmout(struct mg_connection *c, uint8_t type) {
|
static void settmout(struct mg_connection *c, uint8_t type) {
|
||||||
@@ -934,9 +935,9 @@ static void rx_ndp_ns(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
|
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
|
||||||
// - use unicast to verify presence (l2_addr = neighbor l2 address)
|
// - use unicast to verify presence (l2_addr = neighbor l2 address)
|
||||||
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
||||||
uint8_t *l2_addr) {
|
uint8_t *l2_addr, bool ll) {
|
||||||
|
uint64_t *ip_src = ll ? ifp->ip6ll : ifp->ip6;
|
||||||
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
|
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
|
||||||
uint64_t ip_unspec[2] = {0, 0};
|
|
||||||
size_t payload_len = 20;
|
size_t payload_len = 20;
|
||||||
bool mcast = (l2_addr == NULL);
|
bool mcast = (l2_addr == NULL);
|
||||||
uint64_t ip_mcast[2] = {0, 0};
|
uint64_t ip_mcast[2] = {0, 0};
|
||||||
@@ -956,14 +957,12 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
|||||||
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
|
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
|
||||||
}
|
}
|
||||||
payload_len = 20;
|
payload_len = 20;
|
||||||
// TODO(robertc2000): using only link-local IP addr for now
|
if (!MG_IP6UNSPEC(ip_src)) {
|
||||||
// We might consider to add an option to use either link-local or global IP
|
|
||||||
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
|
|
||||||
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
||||||
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
|
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
|
||||||
payload_len += 8 * payload[21];
|
payload_len += 8 * payload[21];
|
||||||
}
|
}
|
||||||
tx_icmp6(ifp, l2, ifp->ip6ll, mcast ? ip_mcast : ip_dst, 135, 0, payload,
|
tx_icmp6(ifp, l2, ip_src, mcast ? ip_mcast : ip_dst, 135, 0, payload,
|
||||||
payload_len);
|
payload_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -971,11 +970,10 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
|
|||||||
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
|
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
|
||||||
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
|
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
|
||||||
size_t payload_len = 4;
|
size_t payload_len = 4;
|
||||||
uint64_t ip_unspec[2] = {0, 0};
|
|
||||||
|
|
||||||
memset(payload, 0, sizeof(payload));
|
memset(payload, 0, sizeof(payload));
|
||||||
|
|
||||||
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
|
if (!MG_IP6UNSPEC(ifp->ip6ll)) {
|
||||||
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
|
||||||
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
|
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
|
||||||
payload_len += 8 * payload[5];
|
payload_len += 8 * payload[5];
|
||||||
@@ -1144,7 +1142,7 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
|
|||||||
(struct mg_addr *) &ip6_allnodes),
|
(struct mg_addr *) &ip6_allnodes),
|
||||||
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
|
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
|
||||||
ifp->mac);
|
ifp->mac);
|
||||||
if (ifp->ip6[0] != 0 && ifp->ip6[1] != 0)
|
if (!MG_IP6UNSPEC(ifp->ip6))
|
||||||
tx_ndp_na(ifp, // RFC-9131 4.1
|
tx_ndp_na(ifp, // RFC-9131 4.1
|
||||||
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
|
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
|
||||||
(struct mg_addr *) &ip6_allrouters),
|
(struct mg_addr *) &ip6_allrouters),
|
||||||
@@ -1159,9 +1157,9 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
|
|||||||
ifp->is_ip6_changed = false;
|
ifp->is_ip6_changed = false;
|
||||||
}
|
}
|
||||||
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
|
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
|
||||||
if ((ifp->gw6[0] != 0 || ifp->gw6[1] != 0) &&
|
if (!MG_IP6UNSPEC(ifp->gw6) &&
|
||||||
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
|
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
|
||||||
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
|
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // unsolicited GW hwaddr resolution
|
||||||
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
||||||
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
|
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
|
||||||
}
|
}
|
||||||
@@ -1374,13 +1372,14 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
|
|||||||
struct pkt *pkt, uint16_t mss) {
|
struct pkt *pkt, uint16_t mss) {
|
||||||
struct connstate *s;
|
struct connstate *s;
|
||||||
uint8_t *l2addr;
|
uint8_t *l2addr;
|
||||||
|
struct mg_tcpip_if *ifp = lsn->mgr->ifp;
|
||||||
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
|
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
|
||||||
if (c == NULL) {
|
if (c == NULL) {
|
||||||
MG_ERROR(("OOM"));
|
MG_ERROR(("OOM"));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
s = (struct connstate *) (c + 1);
|
s = (struct connstate *) (c + 1);
|
||||||
s->retransmit = lsn->mgr->ifp->enable_tcp_retransmit;
|
s->retransmit = ifp->enable_tcp_retransmit;
|
||||||
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
||||||
s->dmss = mss; // from options in client SYN
|
s->dmss = mss; // from options in client SYN
|
||||||
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
|
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
|
||||||
@@ -1389,19 +1388,21 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
|
|||||||
if (lsn->loc.is_ip6) {
|
if (lsn->loc.is_ip6) {
|
||||||
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
||||||
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
||||||
c->loc.addr.ip6[0] = c->mgr->ifp->ip6[0],
|
if (pkt->ip6->dst[0] == ifp->ip6ll[0]) { // link-local prefix
|
||||||
c->loc.addr.ip6[1] = c->mgr->ifp->ip6[1], c->loc.is_ip6 = true;
|
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
// TODO(): compare lsn to link-local, or rem as link-local: use ll instead
|
} else {
|
||||||
|
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
||||||
|
}
|
||||||
|
c->loc.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
c->rem.addr.ip4 = pkt->ip->src;
|
c->rem.addr.ip4 = pkt->ip->src;
|
||||||
c->loc.addr.ip4 = c->mgr->ifp->ip;
|
c->loc.addr.ip4 = ifp->ip;
|
||||||
}
|
}
|
||||||
c->rem.port = pkt->tcp->sport;
|
c->rem.port = pkt->tcp->sport;
|
||||||
c->loc.port = lsn->loc.port;
|
c->loc.port = lsn->loc.port;
|
||||||
if ((l2addr = get_return_l2addr(lsn->mgr->ifp, &c->rem, false, pkt)) ==
|
if ((l2addr = get_return_l2addr(ifp, &c->rem, false, pkt)) == NULL) {
|
||||||
NULL) {
|
|
||||||
mg_free(c); // safety net for lousy networks, not actually needed
|
mg_free(c); // safety net for lousy networks, not actually needed
|
||||||
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
|
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
|
||||||
}
|
}
|
||||||
@@ -1459,10 +1460,14 @@ static bool udp_send(struct mg_connection *c, const void *buf, size_t len) {
|
|||||||
struct mg_addr ips;
|
struct mg_addr ips;
|
||||||
memset(&ips, 0, sizeof(ips));
|
memset(&ips, 0, sizeof(ips));
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->loc.is_ip6) {
|
if (c->loc.is_ip6) { // At [::] infer link-local from peer
|
||||||
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1],
|
if (MG_IP6UNSPEC(c->loc.addr.ip6) ? c->rem.addr.ip6[0] == ifp->ip6ll[0]
|
||||||
|
: c->loc.addr.ip6[0] == ifp->ip6ll[0]) {
|
||||||
|
ips.addr.ip6[0] = ifp->ip6ll[0], ips.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
|
} else {
|
||||||
|
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1];
|
||||||
|
}
|
||||||
ips.is_ip6 = true;
|
ips.is_ip6 = true;
|
||||||
// TODO(): detect link-local (c->rem) and use it
|
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
@@ -2123,14 +2128,14 @@ static void mg_ip6_link(struct mg_tcpip_if *ifp, bool drv_up, bool l2_up) {
|
|||||||
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
|
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
|
||||||
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
|
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
|
||||||
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
|
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
|
||||||
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up && ifp->ip6ll[0] == 0 &&
|
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up &&
|
||||||
ifp->ip6ll[1] == 0) { // gen ll address
|
MG_IP6UNSPEC(ifp->ip6ll)) { // gen ll address
|
||||||
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
|
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
|
||||||
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
|
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
|
||||||
} // just got our link local address if we didn't have one.
|
} // just got our link local address if we didn't have one.
|
||||||
// If static configuration is used, global addresses,
|
// If static configuration is used, global addresses,
|
||||||
// prefix length, and gw are already filled at this point.
|
// prefix length, and gw are already filled at this point.
|
||||||
if (ifp->ip6[0] == 0 && ifp->ip6[1] == 0) ifp->enable_slaac = true;
|
if (MG_IP6UNSPEC(ifp->ip6)) ifp->enable_slaac = true;
|
||||||
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
|
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
|
||||||
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
|
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
|
||||||
: !l2_up ? MG_TCPIP_STATE_LINK_UP
|
: !l2_up ? MG_TCPIP_STATE_LINK_UP
|
||||||
@@ -2191,8 +2196,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
|
|||||||
onstate6change(ifp);
|
onstate6change(ifp);
|
||||||
}
|
}
|
||||||
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
|
||||||
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
|
!MG_IP6UNSPEC(ifp->gw6))
|
||||||
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
|
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // retry GW hwaddr resolution
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{ // poll driver and let L2 do its work, if any
|
{ // poll driver and let L2 do its work, if any
|
||||||
@@ -2371,11 +2376,10 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
} // otherwise retain an existing bound port
|
} // otherwise retain an existing bound port
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->rem.is_ip6) {
|
if (c->rem.is_ip6) {
|
||||||
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
|
bool any = MG_IP6UNSPEC(c->loc.addr.ip6);
|
||||||
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
if (any && c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
} else if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
|
} else if (any) { // Use global address
|
||||||
// Use global address
|
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
||||||
} // otherwise retain an existing bound address
|
} // otherwise retain an existing bound address
|
||||||
c->loc.is_ip6 = true;
|
c->loc.is_ip6 = true;
|
||||||
@@ -2399,7 +2403,8 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
|
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
|
||||||
// If we're in the same LAN, fire a Neighbor Solicitation
|
// If we're in the same LAN, fire a Neighbor Solicitation
|
||||||
MG_DEBUG(("%lu NS lookup...", c->id));
|
MG_DEBUG(("%lu NS lookup...", c->id));
|
||||||
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL); // RFC-4861 4.3, requesting
|
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL,
|
||||||
|
c->loc.addr.ip6[0] == ifp->ip6ll[0]); // RFC-4861 4.3, request
|
||||||
settmout(c, MIP_TTYPE_ARP);
|
settmout(c, MIP_TTYPE_ARP);
|
||||||
c->is_arplooking = 1;
|
c->is_arplooking = 1;
|
||||||
} else if (ifp->gw6_ready) {
|
} else if (ifp->gw6_ready) {
|
||||||
|
|||||||
@@ -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
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
-61
@@ -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
@@ -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>
|
||||||
|
|||||||
@@ -328,6 +328,8 @@ static void init_tests(struct mg_mgr *mgr, struct eth *e, struct ipp *ipp,
|
|||||||
mif->driver_data = &s_driver_data;
|
mif->driver_data = &s_driver_data;
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (ipp->ip6 != NULL) {
|
if (ipp->ip6 != NULL) {
|
||||||
|
mif->ip6ll[0] = mg_htonll(0xfe80000000000000);
|
||||||
|
mif->ip6ll[1] = mg_htonll(1);
|
||||||
mif->ip6[0] = mg_htonll(0x100000000000000);
|
mif->ip6[0] = mg_htonll(0x100000000000000);
|
||||||
mif->prefix[0] = 1;
|
mif->prefix[0] = 1;
|
||||||
mif->prefix_len = 64;
|
mif->prefix_len = 64;
|
||||||
@@ -664,6 +666,16 @@ static void test_tcp_basics(bool ipv6) {
|
|||||||
ASSERT((t->ack == mg_htonl(1002)));
|
ASSERT((t->ack == mg_htonl(1002)));
|
||||||
// make sure it is closed
|
// make sure it is closed
|
||||||
ASSERT(mgr.conns->next == NULL); // only one connection: the listener
|
ASSERT(mgr.conns->next == NULL); // only one connection: the listener
|
||||||
|
#if MG_ENABLE_IPV6
|
||||||
|
// Test we use link-local address for link-local peers
|
||||||
|
if (ipv6) {
|
||||||
|
ip6.src[0] = mif.ip6ll[0], ip6.src[1] = mg_htonll(2);
|
||||||
|
ip6.dst[0] = mif.ip6ll[0], ip6.dst[1] = mif.ip6ll[1];
|
||||||
|
init_tcp_handshake(&e, &ipp, &mgr);
|
||||||
|
ASSERT(mgr.conns->loc.addr.ip6[0] == mif.ip6ll[0] &&
|
||||||
|
mgr.conns->loc.addr.ip6[1] == mif.ip6ll[1]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
s_driver_data.len = 0;
|
s_driver_data.len = 0;
|
||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
@@ -1513,6 +1525,18 @@ static void test_udp(bool ipv6) {
|
|||||||
ASSERT((uint8_t) (((i6->ver & 15) << 2) | (i6->label[0] >> 6)) ==
|
ASSERT((uint8_t) (((i6->ver & 15) << 2) | (i6->label[0] >> 6)) ==
|
||||||
MG_DSCP_AF(4, 1));
|
MG_DSCP_AF(4, 1));
|
||||||
ASSERT(udp6csum_ok(i6, u));
|
ASSERT(udp6csum_ok(i6, u));
|
||||||
|
#if MG_ENABLE_IPV6
|
||||||
|
// Test we use link-local address for link-local peers
|
||||||
|
mgr.conns->recv.len = 0;
|
||||||
|
ip6.src[0] = mif.ip6ll[0], ip6.src[1] = mg_htonll(2);
|
||||||
|
ip6.dst[0] = mif.ip6ll[0], ip6.dst[1] = mif.ip6ll[1];
|
||||||
|
s_driver_data.len = 0;
|
||||||
|
create_udp_dat(&e, &ipp, 1, 888, 2);
|
||||||
|
mg_mgr_poll(&mgr, 0);
|
||||||
|
ASSERT(received_response(&s_driver_data));
|
||||||
|
ASSERT(i6->src[0] == mif.ip6ll[0] && i6->src[1] == mif.ip6ll[1] &&
|
||||||
|
i6->dst[0] == ip6.src[0] && i6->dst[1] == ip6.src[1]);
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
ASSERT(i->src == mg_htonl(0x1000000) && i->dst == mg_htonl(0x2000000));
|
ASSERT(i->src == mg_htonl(0x1000000) && i->dst == mg_htonl(0x2000000));
|
||||||
ASSERT(i->tos == (MG_DSCP_AF(4, 1) << 2));
|
ASSERT(i->tos == (MG_DSCP_AF(4, 1) << 2));
|
||||||
|
|||||||
+17
-1
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in new issue
Block a user