Skip to content

Commit

Permalink
Pico Micropython wih UART REPL and USB networking
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermann-SW committed Apr 21, 2021
1 parent 321d189 commit f782c8b
Show file tree
Hide file tree
Showing 17 changed files with 4,897 additions and 5 deletions.
16 changes: 15 additions & 1 deletion ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ include(${MICROPY_DIR}/py/py.cmake)
include(${MICROPY_DIR}/extmod/extmod.cmake)
include(${PICO_SDK_PATH}/pico_sdk_init.cmake)

# LWIP
set(LWIP_DIR ${MICROPY_DIR}/lib/lwip)
set (LWIP_INCLUDE_DIRS
"${LWIP_DIR}/src/include"
"${CMAKE_CURRENT_SOURCE_DIR}"
)
include(${LWIP_DIR}/src/Filelists.cmake)

# Define the top-level project
project(${MICROPY_TARGET})

Expand Down Expand Up @@ -167,14 +175,20 @@ target_sources(${MICROPY_TARGET} PRIVATE
${MICROPY_SOURCE_LIB}
${MICROPY_SOURCE_DRIVERS}
${MICROPY_SOURCE_PORT}
tusb_lwip_glue.c
usb_descriptors.c
${PICO_TINYUSB_PATH}/lib/networking/dhserver.c
${PICO_TINYUSB_PATH}/lib/networking/rndis_reports.c
)

target_link_libraries(${MICROPY_TARGET} usermod)
target_link_libraries(${MICROPY_TARGET} usermod lwipallapps lwipcore)

target_include_directories(${MICROPY_TARGET} PRIVATE
${MICROPY_INC_CORE}
${MICROPY_INC_USERMOD}
${MICROPY_BOARD_DIR}
${PICO_TINYUSB_PATH}/lib/networking
${LWIP_DIR}/src/include
"${PROJECT_SOURCE_DIR}"
"${CMAKE_BINARY_DIR}"
)
Expand Down
77 changes: 77 additions & 0 deletions ports/rp2/arch/cc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#ifndef __CC_H__
#define __CC_H__

//#include "cpu.h"

typedef int sys_prot_t;



/* define compiler specific symbols */
#if defined (__ICCARM__)

#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x
#define PACK_STRUCT_USE_INCLUDES

#elif defined (__CC_ARM)

#define PACK_STRUCT_BEGIN __packed
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x

#elif defined (__GNUC__)

#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT __attribute__ ((__packed__))
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x

#elif defined (__TASKING__)

#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x

#endif

#ifndef LWIP_PLATFORM_ASSERT
#define LWIP_PLATFORM_ASSERT(x) do { if(!(x)) while(1); } while(0)
#endif

#endif /* __CC_H__ */
Binary file added ports/rp2/bin/firmware.uf2
Binary file not shown.
22 changes: 22 additions & 0 deletions ports/rp2/fs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Pico Micropython (UART REPL + USB networking!)</title>
</head>
<body>

<center>
<h1>Pico MicroPython (UART REPL + USB networking!)</h1>

<!-- Image Map Generated by http://www.image-map.net/ -->
<nobr><img src="pico.png"> <img src="uPython.png"></nobr>

<br><br>
Networking and webserver running completely on core1 (lwIP "sys_timeouts_init()" has to be called on core0). <p>
MicroPython runs on core0 (USB REPL as well as thread support disabled).

</center>
</body>
</html>
Binary file added ports/rp2/fs/pico.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ports/rp2/fs/uPython.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4,074 changes: 4,074 additions & 0 deletions ports/rp2/fsdata.c

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions ports/rp2/lwipopts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Simon Goldschmidt
*
*/
#ifndef __LWIPOPTS_H__
#define __LWIPOPTS_H__

/* Prevent having to link sys_arch.c (we don't test the API layers in unit tests) */
#define NO_SYS 1
#define MEM_ALIGNMENT 4
#define LWIP_RAW 0
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0
#define LWIP_DHCP 0
#define LWIP_ICMP 1
#define LWIP_UDP 1
#define LWIP_TCP 1
#define ETH_PAD_SIZE 0
#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))

#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (2 * TCP_MSS)

#define ETHARP_SUPPORT_STATIC_ENTRIES 1

#define LWIP_HTTPD_CGI 1
#ifndef LWIP_HTTPD_SSI
#define LWIP_HTTPD_SSI 0
#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
#endif
#define HTTPD_USE_CUSTOM_FSDATA 1
#define HTTPD_FSDATA_FILE "../../../../../ports/rp2/fsdata.c"

#define LWIP_SINGLE_NETIF 1

#endif /* __LWIPOPTS_H__ */
25 changes: 24 additions & 1 deletion ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@
#include "hardware/rtc.h"
#include "hardware/structs/rosc.h"

#include "pico/multicore.h"

#include "tusb_lwip_glue.h"

extern uint8_t __StackTop, __StackBottom;
static char gc_heap[192 * 1024];
static char gc_heap[189 * 1024];

// Embed version info in the binary in machine readable form
bi_decl(bi_program_version_string(MICROPY_GIT_TAG));
Expand All @@ -58,6 +62,21 @@ bi_decl(bi_program_feature_group_with_flags(BINARY_INFO_TAG_MICROPYTHON,
BINARY_INFO_ID_MP_FROZEN, "frozen modules",
BI_NAMED_GROUP_SEPARATE_COMMAS | BI_NAMED_GROUP_SORT_ALPHA));

void core1_entry()
{
// Initialize tinyusb, lwip, dhcpd and httpd
init_lwip();
wait_for_netif_is_up();
dhcpd_init();
httpd_init();

while (true)
{
tud_task();
service_traffic();
}
}

int main(int argc, char **argv) {
#if MICROPY_HW_ENABLE_UART_REPL
bi_decl(bi_program_feature("UART REPL"))
Expand All @@ -75,6 +94,10 @@ int main(int argc, char **argv) {
mp_thread_init();
#endif

sys_timeouts_init();

multicore_launch_core1(core1_entry);

// Start and initialise the RTC
datetime_t t = {
.year = 2021,
Expand Down
6 changes: 3 additions & 3 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

// Board and hardware specific configuration
#define MICROPY_HW_MCU_NAME "RP2040"
#define MICROPY_HW_ENABLE_UART_REPL (0) // useful if there is no USB
#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_HW_ENABLE_UART_REPL (1) // useful if there is no USB
#define MICROPY_HW_ENABLE_USBDEV (0)

// Memory allocation policies
#define MICROPY_GC_STACK_ENTRY_TYPE uint16_t
Expand Down Expand Up @@ -106,7 +106,7 @@
#define MICROPY_PY_SYS_STDIO_BUFFER (1)
#define MICROPY_PY_SYS_PLATFORM "rp2"
#define MICROPY_PY_UERRNO (1)
#define MICROPY_PY_THREAD (1)
#define MICROPY_PY_THREAD (0)
#define MICROPY_PY_THREAD_GIL (0)

// Extended modules
Expand Down
4 changes: 4 additions & 0 deletions ports/rp2/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ int mp_interrupt_char = -1;
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
(void)itf;
(void)wanted_char;
#if MICROPY_HW_ENABLE_USBDEV
tud_cdc_read_char(); // discard interrupt char
#endif
mp_keyboard_interrupt();
}

void mp_hal_set_interrupt_char(int c) {
mp_interrupt_char = c;
#if MICROPY_HW_ENABLE_USBDEV
tud_cdc_set_wanted_char(c);
#endif
}

#endif
Expand Down
11 changes: 11 additions & 0 deletions ports/rp2/regen-fsdata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

if [ ! -f makefsdata ]; then
# Doing this outside cmake as we don't want it cross-compiled but for host
echo Compiling makefsdata
gcc -o makefsdata -I../../lib/lwip/src/include -I. ../../lib/lwip/src/apps/http/makefsdata/makefsdata.c
fi

echo Regenerating fsdata.c
./makefsdata
echo Done
87 changes: 87 additions & 0 deletions ports/rp2/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,91 @@
#define CFG_TUD_CDC_RX_BUFSIZE (256)
#define CFG_TUD_CDC_TX_BUFSIZE (256)


#ifdef __cplusplus
extern "C" {
#endif

//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------

// defined by board.mk
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif

// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_DEVICE_RHPORT_NUM
#define BOARD_DEVICE_RHPORT_NUM 0
#endif

// RHPort max operational speed can defined by board.mk
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
#ifndef BOARD_DEVICE_RHPORT_SPEED
#if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56)
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED
#else
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
#endif
#endif

// Device mode with rhport and speed defined by board.mk
#if BOARD_DEVICE_RHPORT_NUM == 0
#ifdef CFG_TUSB_RHPORT0_MODE
#undef CFG_TUSB_RHPORT0_MODE
#endif
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#elif BOARD_DEVICE_RHPORT_NUM == 1
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#else
#error "Incorrect RHPort configuration"
#endif

#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE
#endif

// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
// #define CFG_TUSB_DEBUG 0

/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put
* into those specific section.
* e.g
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/
#ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION
#endif

#ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
#endif

//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------

#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif

//------------- CLASS -------------//
#ifdef CFG_TUD_CDC
#undef CFG_TUD_CDC
#endif
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0
#define CFG_TUD_NET 1

#ifdef __cplusplus
}
#endif

#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H

0 comments on commit f782c8b

Please sign in to comment.