Move mip into mip/

This commit is contained in:
Sergey Lyubka 2022-06-11 17:29:45 +01:00
parent 903b510411
commit d3368e4aad
13 changed files with 1016 additions and 858 deletions

View File

@ -1,5 +1,5 @@
SRCS = mongoose.c test/unit_test.c test/packed_fs.c
HDRS = $(wildcard src/*.h)
HDRS = $(wildcard src/*.h) $(wildcard mip/*.h)
DEFS ?= -DMG_MAX_HTTP_HEADERS=7 -DMG_ENABLE_LINES -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_SSI=1
C_WARN ?= -Wmissing-prototypes -Wstrict-prototypes
WARN ?= -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common -Wconversion -Wundef $(C_WARN)
@ -149,11 +149,11 @@ install: linux-libs
uninstall:
rm -rf $(DESTDIR)$(PREFIX)/lib/libmongoose.a $(DESTDIR)$(PREFIX)/lib/libmongoose.so.$(VERSION) $(DESTDIR)$(PREFIX)/include/mongoose.h $(DESTDIR)$(PREFIX)/lib/libmongoose.so
mongoose.c: Makefile $(wildcard src/*)
(cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
mongoose.c: Makefile $(wildcard src/*) $(wildcard mip/*.c)
(cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c mip/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
mongoose.h: $(HDRS) Makefile
(cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/mip.h src/json.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
(cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h mip/mip.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
clean:
rm -rf $(PROG) *.exe *.o *.dSYM unit_test* valgrind_unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack unpacked

View File

@ -1,2 +0,0 @@
# This is a collection of low-level drivers for the experimental
# built-in TCP/IP stack

View File

@ -1,12 +0,0 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved
#pragma once
#include <stdbool.h>
#include <stddef.h>
void mip_driver_stm32_init(void *userdata);
bool mip_driver_stm32_status(void *);
void mip_driver_stm32_setrx(void (*rx)(void *, size_t, void *), void *);
size_t mip_driver_stm32_tx(const void *buf, size_t len, void *);

View File

@ -10,8 +10,7 @@ CFLAGS ?= -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion \
-mcpu=cortex-m7 -mthumb -mfloat-abi=softfp -mfpu=vfpv4 $(EXTRA)
LDFLAGS ?= -Tlink.ld -nostartfiles -nostdlib --specs nano.specs \
-lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map
SOURCES = boot.c main.c syscalls.c $(ROOT)/drivers/mip_driver_stm32.c \
$(ROOT)/mongoose.c
SOURCES = boot.c main.c syscalls.c $(ROOT)/mongoose.c
all build example: $(TARGET).bin

View File

@ -2,8 +2,6 @@
// All rights reserved
#include "mcu.h"
#include "drivers/mip_driver_stm32.h"
#include "mongoose.h"
#define LED1 PIN('B', 0) // On-board LED pin (green)
@ -138,11 +136,7 @@ int main(void) {
// For static configuration, specify IP/mask/GW in network byte order
struct mip_ipcfg ipcfg = {
.mac = {0xaa, 0xbb, 0xcc, 1, 2, 3}, .ip = 0, .mask = 0, .gw = 0};
struct mip_driver stm32_driver = {.init = mip_driver_stm32_init,
.tx = mip_driver_stm32_tx,
.rxcb = mip_driver_stm32_setrx,
.status = mip_driver_stm32_status};
mip_init(&mgr, &ipcfg, &stm32_driver);
mip_init(&mgr, &ipcfg, &mip_driver_stm32);
MG_INFO(("Init done, starting main loop"));
#if defined(DASH)

View File

@ -11,6 +11,10 @@ static int s_rx = 4;
static int s_tx = 5;
static int s_baud = 115200;
void uart_init(int tx, int rx, int baud);
int uart_read(char *buf, size_t len);
void uart_write(const void *buf, size_t len);
// Let users define their own UART API. If they don't, use a dummy one
#if defined(UART_API_IMPLEMENTED)
#else

9
mip/README.md Normal file
View File

@ -0,0 +1,9 @@
# MIP - experimental TCP/IP stack for Mongoose Library
This bare-metal embedded TCP/IP stack is designed specifically for Mongoose.
It does not implement BSD sockets interface - instead, it implements
an event-based API tailored for Mongoose Library. As a result, it does not
need to use synchronisation primitives. It is very fast, and can work
with or without RTOS.
See example usage at [examples/stm32/nucleo-f746zg/baremetal](../examples/stm32/nucleo-f746zg/baremetal)

View File

@ -1,12 +1,11 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved
#include "mip_driver_stm32.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "mip.h"
struct eth {
#if MG_ENABLE_MIP
struct stm32_eth {
uint32_t MACCR, MACFFR, MACHTHR, MACHTLR, MACMIIAR, MACMIIDR, MACFCR,
MACVLANTR, RESERVED0[2], MACRWUFFR, MACPMTCSR, RESERVED1, MACDBGR, MACSR,
MACIMR, MACA0HR, MACA0LR, MACA1HR, MACA1LR, MACA2HR, MACA2LR, MACA3HR,
@ -19,9 +18,9 @@ struct eth {
DMAMFBOCR, DMARSWTR, RESERVED10[8], DMACHTDR, DMACHRDR, DMACHTBAR,
DMACHRBAR;
};
#define ETH ((struct eth *) 0x40028000)
#define ETH ((struct stm32_eth *) (uintptr_t) 0x40028000)
#define BIT(x) (1UL << (x))
#define BIT(x) ((uint32_t) 1 << (x))
#define ETH_PKT_SIZE 1540 // Max frame size
#define ETH_DESC_CNT 4 // Descriptors count
#define ETH_DS 4 // Descriptor size (words)
@ -39,7 +38,7 @@ static inline void spin(volatile uint32_t count) {
while (count--) asm("nop");
}
uint32_t eth_read_phy(uint8_t addr, uint8_t reg) {
static uint32_t eth_read_phy(uint8_t addr, uint8_t reg) {
ETH->MACMIIAR &= (7 << 2);
ETH->MACMIIAR |= ((uint32_t) addr << 11) | ((uint32_t) reg << 6);
ETH->MACMIIAR |= BIT(0);
@ -47,7 +46,7 @@ uint32_t eth_read_phy(uint8_t addr, uint8_t reg) {
return ETH->MACMIIDR;
}
void eth_write_phy(uint8_t addr, uint8_t reg, uint32_t val) {
static void eth_write_phy(uint8_t addr, uint8_t reg, uint32_t val) {
ETH->MACMIIDR = val;
ETH->MACMIIAR &= (7 << 2);
ETH->MACMIIAR |= ((uint32_t) addr << 11) | ((uint32_t) reg << 6) | BIT(1);
@ -55,21 +54,23 @@ void eth_write_phy(uint8_t addr, uint8_t reg, uint32_t val) {
while (ETH->MACMIIAR & BIT(0)) spin(1);
}
void mip_driver_stm32_init(void *userdata) {
static void mip_driver_stm32_init(void *userdata) {
s_userdata = userdata;
// Init RX descriptors
for (int i = 0; i < ETH_DESC_CNT; i++) {
s_rxdesc[i][0] = BIT(31); // Own
s_rxdesc[i][1] = sizeof(s_rxbuf[i]) | BIT(14); // 2nd address chained
s_rxdesc[i][2] = (uint32_t) s_rxbuf[i]; // Point to data buffer
s_rxdesc[i][3] = (uint32_t) s_rxdesc[(i + 1) % ETH_DESC_CNT]; // Chain
s_rxdesc[i][0] = BIT(31); // Own
s_rxdesc[i][1] = sizeof(s_rxbuf[i]) | BIT(14); // 2nd address chained
s_rxdesc[i][2] = (uint32_t) (uintptr_t) s_rxbuf[i]; // Point to data buffer
s_rxdesc[i][3] =
(uint32_t) (uintptr_t) s_rxdesc[(i + 1) % ETH_DESC_CNT]; // Chain
}
// Init TX descriptors
for (int i = 0; i < ETH_DESC_CNT; i++) {
s_txdesc[i][2] = (uint32_t) s_txbuf[i]; // Buf pointer
s_txdesc[i][3] = (uint32_t) s_txdesc[(i + 1) % ETH_DESC_CNT]; // Chain
s_txdesc[i][2] = (uint32_t) (uintptr_t) s_txbuf[i]; // Buf pointer
s_txdesc[i][3] =
(uint32_t) (uintptr_t) s_txdesc[(i + 1) % ETH_DESC_CNT]; // Chain
}
ETH->DMABMR |= BIT(0); // Software reset
@ -83,20 +84,21 @@ void mip_driver_stm32_init(void *userdata) {
ETH->MACFFR = BIT(31); // Receive all
eth_write_phy(PHY_ADDR, PHY_BCR, BIT(15)); // Reset PHY
eth_write_phy(PHY_ADDR, PHY_BCR, BIT(12)); // Set autonegotiation
ETH->DMARDLAR = (uint32_t) s_rxdesc; // RX descriptors
ETH->DMATDLAR = (uint32_t) s_txdesc; // RX descriptors
ETH->DMAIER = BIT(6) | BIT(16); // RIE, NISE
ETH->DMARDLAR = (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors
ETH->DMATDLAR = (uint32_t) (uintptr_t) s_txdesc; // RX descriptors
ETH->DMAIER = BIT(6) | BIT(16); // RIE, NISE
ETH->MACCR = BIT(2) | BIT(3) | BIT(11) | BIT(14); // RE, TE, Duplex, Fast
ETH->DMAOMR = BIT(1) | BIT(13) | BIT(21) | BIT(25); // SR, ST, TSF, RSF
}
void mip_driver_stm32_setrx(void (*rx)(void *, size_t, void *), void *rxdata) {
static void mip_driver_stm32_setrx(void (*rx)(void *, size_t, void *),
void *rxdata) {
s_rx = rx;
s_rxdata = rxdata;
}
static uint32_t s_txno;
size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) {
static size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) {
if (len > sizeof(s_txbuf[s_txno])) {
printf("%s: frame too big, %ld\n", __func__, (long) len);
len = 0; // Frame is too big
@ -113,17 +115,18 @@ size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) {
uint32_t sr = ETH->DMASR;
if (sr & BIT(2)) ETH->DMASR = BIT(2), ETH->DMATPDR = 0; // Resume
if (sr & BIT(5)) ETH->DMASR = BIT(5), ETH->DMATPDR = 0; // if busy
if (len == 0) printf("E: D0 %lx, DMASR %lx\n", s_txdesc[0][0], sr);
if (len == 0) printf("E: D0 %lx SR %lx\n", (long) s_txdesc[0][0], (long) sr);
return len;
(void) userdata;
}
bool mip_driver_stm32_status(void *userdata) {
static bool mip_driver_stm32_status(void *userdata) {
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
return bsr & BIT(2) ? 1 : 0;
(void) userdata;
}
void ETH_IRQHandler(void);
void ETH_IRQHandler(void) {
volatile uint32_t sr = ETH->DMASR;
if (sr & BIT(6)) { // Frame received, loop
@ -138,3 +141,9 @@ void ETH_IRQHandler(void) {
if (sr & BIT(7)) ETH->DMARPDR = 0; // Resume RX
ETH->DMASR = sr & ~(BIT(2) | BIT(7)); // Clear status
}
struct mip_driver mip_driver_stm32 = {.init = mip_driver_stm32_init,
.tx = mip_driver_stm32_tx,
.rxcb = mip_driver_stm32_setrx,
.status = mip_driver_stm32_status};
#endif // MG_ENABLE_MIP

View File

@ -19,3 +19,5 @@ struct mip_ipcfg {
};
void mip_init(struct mg_mgr *, struct mip_ipcfg *, struct mip_driver *);
extern struct mip_driver mip_driver_stm32;

1737
mongoose.c

File diff suppressed because it is too large Load Diff

View File

@ -1290,6 +1290,23 @@ size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
#ifndef MG_JSON_MAX_DEPTH
#define MG_JSON_MAX_DEPTH 30
#endif
// Error return values - negative. Successful returns are >= 0
enum { MG_JSON_TOO_DEEP = -1, MG_JSON_INVALID = -2, MG_JSON_NOT_FOUND = -3 };
int mg_json_get(const char *buf, int len, const char *path, int *toklen);
bool mg_json_get_num(struct mg_str json, const char *path, double *v);
bool mg_json_get_bool(struct mg_str json, const char *path, bool *v);
char *mg_json_get_str(struct mg_str json, const char *path);
char *mg_json_get_hex(struct mg_str json, const char *path);
struct mip_driver {
void *data; // Driver-specific data
void (*init)(void *data); // Initialise driver
@ -1307,22 +1324,7 @@ struct mip_ipcfg {
void mip_init(struct mg_mgr *, struct mip_ipcfg *, struct mip_driver *);
#ifndef MG_JSON_MAX_DEPTH
#define MG_JSON_MAX_DEPTH 30
#endif
// Error return values - negative. Successful returns are >= 0
enum { MG_JSON_TOO_DEEP = -1, MG_JSON_INVALID = -2, MG_JSON_NOT_FOUND = -3 };
int mg_json_get(const char *buf, int len, const char *path, int *toklen);
bool mg_json_get_num(struct mg_str json, const char *path, double *v);
bool mg_json_get_bool(struct mg_str json, const char *path, bool *v);
char *mg_json_get_str(struct mg_str json, const char *path);
char *mg_json_get_hex(struct mg_str json, const char *path);
extern struct mip_driver mip_driver_stm32;
#ifdef __cplusplus
}