Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stm32: Incorporate lwip stack and use Wiznet in MACRAW mode #3379

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions drivers/wiznet5k/ethernet/w5200/w5200.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@

#include "w5200.h"

#define SMASK (0x7ff) /* tx buffer mask */
#define RMASK (0x7ff) /* rx buffer mask */
#define SSIZE (2048) /* max tx buffer size */
#define RSIZE (2048) /* max rx buffer size */
#define SMASK (16*1024-1) /* tx buffer mask */
#define RMASK (16*1024-1) /* rx buffer mask */
#define SSIZE (16*1024) /* max tx buffer size */
#define RSIZE (16*1024) /* max rx buffer size */

#define TXBUF_BASE (0x8000)
#define RXBUF_BASE (0xc000)
Expand Down
21 changes: 19 additions & 2 deletions ports/stm32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ include boards/$(BOARD)/mpconfigboard.mk
# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h $(BUILD)/modstm_qstr.h

# extmod modules
MICROPY_PY_USSL = 1
MICROPY_SSL_AXTLS = 1

# directory containing scripts to be frozen as bytecode
FROZEN_MPY_DIR ?= modules

Expand Down Expand Up @@ -86,8 +90,14 @@ CFLAGS += -DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_FLOAT
CFLAGS += -fsingle-precision-constant -Wdouble-promotion
endif

ifeq ($(MICROPY_PY_USSL),1)
ifeq ($(MICROPY_SSL_AXTLS),1)
LIBS += -L$(BUILD) -laxtls
endif
endif

LDFLAGS = -nostdlib -L $(LD_DIR) $(addprefix -T,$(LD_FILES)) -Map=$(@:.elf=.map) --cref
LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)

# Remove uncalled code from the final image.
CFLAGS += -fdata-sections -ffunction-sections
Expand Down Expand Up @@ -429,7 +439,7 @@ $(PY_BUILD)/formatfloat.o: COPT += -Os
$(PY_BUILD)/parsenum.o: COPT += -Os
$(PY_BUILD)/mpprint.o: COPT += -Os

all: $(TOP)/lib/stm32lib/README.md $(BUILD)/firmware.dfu $(BUILD)/firmware.hex
all: $(TOP)/lib/stm32lib/README.md $(BUILD)/libaxtls.a $(BUILD)/firmware.dfu $(BUILD)/firmware.hex

# For convenience, automatically fetch required submodules if they don't exist
$(TOP)/lib/stm32lib/README.md:
Expand Down Expand Up @@ -584,4 +594,11 @@ $(GEN_CDCINF_FILE): $(CDCINF_TEMPLATE) $(INSERT_USB_IDS) $(USB_IDS_FILE) | $(HEA
$(ECHO) "GEN $@"
$(Q)$(PYTHON) $(INSERT_USB_IDS) $(USB_IDS_FILE) $< > $@

$(BUILD)/libaxtls.a: | $(BUILD)/
cd $(TOP)/lib/axtls; cp config/upyconfig config/.config
cd $(TOP)/lib/axtls; $(MAKE) oldconfig -B
cd $(TOP)/lib/axtls; $(MAKE) clean
cd $(TOP)/lib/axtls; $(MAKE) all CC="$(CC)" LD="$(LD)" AR="$(AR)" CFLAGS_EXTRA="$(CFLAGS_MCU_$(MCU_SERIES)) -I../../../ports/stm32"
cp $(TOP)/lib/axtls/_stage/libaxtls.a $@

include $(TOP)/py/mkrules.mk
1 change: 1 addition & 0 deletions ports/stm32/arpa/inet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty, for axtls
69 changes: 69 additions & 0 deletions ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,72 @@ void stm32_main(uint32_t reset_mode) {

goto soft_reset;
}

/*******************************************************************/
// Support functions needed by axtls

#include <time.h>
#include <sys/time.h>

#undef malloc
#undef free
#undef realloc

char *strncpy(char *dest, const char *src, size_t n) {
size_t i;
for (i = 0; i < n && src[i] != '\0'; i++) {
dest[i] = src[i];
}
for ( ; i < n; i++) {
dest[i] = '\0';
}
return dest;
}

void *malloc(size_t size) {
return m_malloc(size);
}
void free(void *ptr) {
m_free(ptr);
}
void *calloc(size_t nmemb, size_t size) {
return m_malloc(nmemb * size);
}
void *realloc(void *ptr, size_t size) {
return m_realloc(ptr, size);
}
void abort(void) {
mp_raise_msg(&mp_type_RuntimeError, "abort() called");
}

int rand(void) {
return rng_get();
}
int rand_r(unsigned int *seedp) {
// hopefully we can get away with this with lwIP...
return 0;
}

time_t time(time_t *t) {
return mp_hal_ticks_ms() / 1000;
}
time_t mktime(struct tm *tm) {
// TODO
return 0;
}
int gettimeofday(struct timeval *restrict tp, void *restrict tzp) {
uint32_t ms = mp_hal_ticks_ms();
tp->tv_sec = ms / 1000;
tp->tv_usec = ms % 1000 * 1000;
return 0;
}

#define PLATFORM_HTONL(_n) ((uint32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) ))
#undef htonl
#undef ntohl
uint32_t ntohl(uint32_t netlong) {
return PLATFORM_HTONL(netlong);
}
uint32_t htonl(uint32_t netlong) {
return PLATFORM_HTONL(netlong);
}
1 change: 1 addition & 0 deletions ports/stm32/modules/uasyncio/__init__.py
1 change: 1 addition & 0 deletions ports/stm32/modules/uasyncio/core.py
1 change: 1 addition & 0 deletions ports/stm32/modules/upip.py
1 change: 1 addition & 0 deletions ports/stm32/modules/upip_utarfile.py
1 change: 1 addition & 0 deletions ports/stm32/modules/urequests.py
6 changes: 6 additions & 0 deletions ports/stm32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
#endif
#define MICROPY_STREAMS_NON_BLOCK (1)
#define MICROPY_STREAMS_POSIX_API (1)
#define MICROPY_MODULE_WEAK_LINKS (1)
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
#define MICROPY_USE_INTERNAL_ERRNO (1)
Expand Down Expand Up @@ -132,6 +133,7 @@
#define MICROPY_PY_UTIMEQ (1)
#define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_PY_OS_DUPTERM (1)
#define MICROPY_PY_LWIP (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_PULSE (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
Expand Down Expand Up @@ -297,6 +299,10 @@ typedef int mp_int_t; // must be pointer size
typedef unsigned int mp_uint_t; // must be pointer size
typedef long mp_off_t;

// ssize_t, off_t as required by POSIX-signatured functions in stream.h
typedef int ssize_t;
typedef mp_off_t off_t;

#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)

// We have inlined IRQ functions for efficiency (they are generally
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# 0 : no Wiznet support
# 5200 : support for W5200 module
# 5500 : support for W5500 module
MICROPY_PY_WIZNET5K ?= 0
MICROPY_PY_WIZNET5K ?= 5500

# cc3k module for wifi support
MICROPY_PY_CC3K ?= 0
8 changes: 8 additions & 0 deletions ports/stm32/wiznet_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import time, network
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
nic.active(1)
while not nic.isconnected():
time.sleep_ms(50) # needed to poll the NIC
print(nic.ifconfig())
import socket
print(socket.getaddrinfo('micropython.org', 80))