├── .gitmodules ├── LICENSE ├── README.md ├── boards ├── CUSTOM_ESP32 │ ├── CMakeLists.txt │ ├── README.md │ ├── main │ │ ├── CMakeLists.txt │ │ └── idf_component.yml │ ├── manifest.py │ ├── mpconfigboard.cmake │ ├── mpconfigboard.h │ └── partitions-4MiB.csv ├── CUSTOM_PYBD_SF2 │ ├── Makefile │ ├── README.md │ ├── bdev.c │ ├── board_init.c │ ├── manifest.py │ ├── mpconfigboard.h │ ├── mpconfigboard.mk │ ├── pins.csv │ └── stm32f7xx_hal_conf.h ├── CUSTOM_PYBV11 │ ├── Makefile │ ├── README.md │ ├── board_init.c │ ├── manifest.py │ ├── mpconfigboard.h │ ├── mpconfigboard.mk │ ├── pins.csv │ └── stm32f4xx_hal_conf.h └── README.md └── src ├── README.md ├── cmodules ├── cexample │ ├── micropython.cmake │ ├── micropython.mk │ └── modcexample.c └── cexample2 │ ├── micropython.cmake │ ├── micropython.mk │ └── modcexample2.c └── utils └── xxd.py /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "lib/micropython"] 2 | path = lib/micropython 3 | url = https://github.com/micropython/micropython.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 Damien P. George 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MicroPython example boards 2 | ========================== 3 | 4 | This repository is an example of how to define custom MicroPython boards 5 | with custom Python and C modules. The core MicroPython repository is a 6 | submodule and other code, including board definitions, is part of this 7 | repository. 8 | 9 | To set up MicroPython, check out the submodule and build `mpy-cross`: 10 | 11 | $ git submodule update --init lib/micropython 12 | $ make -C lib/micropython/mpy-cross 13 | 14 | The various custom boards will require certain submodules of MicroPython 15 | itself. These can be initialised via: 16 | 17 | $ cd lib/micropython 18 | $ git submodule update --init lib/stm32lib lib/berkeley-db-1.xx 19 | $ cd ../.. 20 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Top-level cmake file for building MicroPython on ESP32. 2 | # Nothing in here needs to be customised, it will work for any board. 3 | 4 | cmake_minimum_required(VERSION 3.12) 5 | 6 | # Set the location of MicroPython, the esp32 port, and the board directory. 7 | get_filename_component(PROJECT_DIR "../.." ABSOLUTE) 8 | #get_filename_component(MICROPY_TOP "../../lib/micropython" ABSOLUTE) 9 | #set(MICROPY_PORT_DIR ${MICROPY_TOP}/ports/esp32) 10 | set(MICROPY_PORT_DIR ${PROJECT_DIR}/lib/micropython/ports/esp32) 11 | get_filename_component(MICROPY_BOARD_DIR "." ABSOLUTE) 12 | 13 | # Define the output sdkconfig so it goes in the build directory. 14 | set(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig) 15 | 16 | # Include board config; this is expected to set SDKCONFIG_DEFAULTS (among other options). 17 | include(${MICROPY_BOARD_DIR}/mpconfigboard.cmake) 18 | 19 | # Concatenate all sdkconfig files into a combined one for the IDF to use. 20 | file(WRITE ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "") 21 | foreach(SDKCONFIG_DEFAULT ${SDKCONFIG_DEFAULTS}) 22 | file(READ ${SDKCONFIG_DEFAULT} CONTENTS) 23 | file(APPEND ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "${CONTENTS}") 24 | endforeach() 25 | configure_file(${CMAKE_BINARY_DIR}/sdkconfig.combined.in ${CMAKE_BINARY_DIR}/sdkconfig.combined COPYONLY) 26 | set(SDKCONFIG_DEFAULTS ${CMAKE_BINARY_DIR}/sdkconfig.combined) 27 | 28 | # Include main IDF cmake file and define the project. 29 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 30 | project(micropython) 31 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/README.md: -------------------------------------------------------------------------------- 1 | This directory contains board definition files for an esp32-based board. 2 | 3 | To build it make sure the IDF has been installed and the environment set up, 4 | for example using `export $IDF_PATH/export.sh`. Then build with: 5 | 6 | $ idf.py build 7 | 8 | To deploy the firmware to the board: 9 | 10 | $ idf.py -p /dev/ttyUSB0 flash 11 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Include MicroPython ESP32 component. 2 | get_filename_component(MICROPY_DIR "../../../lib/micropython" ABSOLUTE) 3 | set(PROJECT_DIR ${MICROPY_DIR}/ports/esp32) 4 | include(${PROJECT_DIR}/main_esp32/CMakeLists.txt) 5 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/main/idf_component.yml: -------------------------------------------------------------------------------- 1 | ## IDF Component Manager Manifest File 2 | dependencies: 3 | espressif/mdns: "~1.1.0" 4 | idf: 5 | version: ">=5.0.2" 6 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/manifest.py: -------------------------------------------------------------------------------- 1 | # include default manifest 2 | include("$(PORT_DIR)/boards/manifest.py") 3 | 4 | # include our own extra... 5 | module("xxd.py", base_path="$(BOARD_DIR)/../../src/utils") 6 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/mpconfigboard.cmake: -------------------------------------------------------------------------------- 1 | # Define the chip variant. 2 | set(IDF_TARGET esp32) 3 | 4 | # Set the sdkconfig fragments. 5 | set(SDKCONFIG_DEFAULTS 6 | ${MICROPY_PORT_DIR}/boards/sdkconfig.base 7 | ${MICROPY_PORT_DIR}/boards/sdkconfig.ble 8 | ) 9 | 10 | # Set the user C modules to include in the build. 11 | set(USER_C_MODULES 12 | ${PROJECT_DIR}/src/cmodules/cexample/micropython.cmake 13 | ${PROJECT_DIR}/src/cmodules/cexample2/micropython.cmake 14 | ) 15 | 16 | # Set the manifest file for frozen Python code. 17 | set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) 18 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/mpconfigboard.h: -------------------------------------------------------------------------------- 1 | #define MICROPY_HW_BOARD_NAME "Custom ESP32 board" 2 | #define MICROPY_HW_MCU_NAME "ESP32" 3 | -------------------------------------------------------------------------------- /boards/CUSTOM_ESP32/partitions-4MiB.csv: -------------------------------------------------------------------------------- 1 | # Notes: the offset of the partition table itself is set in 2 | # $IDF_PATH/components/partition_table/Kconfig.projbuild. 3 | # Name, Type, SubType, Offset, Size, Flags 4 | nvs, data, nvs, 0x9000, 0x6000, 5 | phy_init, data, phy, 0xf000, 0x1000, 6 | factory, app, factory, 0x10000, 0x1F0000, 7 | vfs, data, fat, 0x200000, 0x200000, 8 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for custom MicroPython stm32 board. 2 | 3 | ################################################################################ 4 | # Define your settings here. 5 | 6 | # The board name. 7 | BOARD ?= CUSTOM_PYBD_SF2 8 | 9 | # Location of MicroPython repository. 10 | MICROPY_TOP ?= $(abspath ../../lib/micropython) 11 | 12 | ################################################################################ 13 | # Define your targets here. 14 | 15 | all: firmware 16 | 17 | ################################################################################ 18 | # Items below this line do not generally need to be changed. 19 | 20 | BOARD_DIR = $(abspath .) 21 | BUILD = $(abspath build) 22 | 23 | include $(MICROPY_TOP)/py/mkenv.mk 24 | include $(MICROPY_TOP)/py/mkrules.mk 25 | 26 | firmware: 27 | $(Q)$(MAKE) -C $(MICROPY_TOP)/ports/stm32 \ 28 | PROJECT_TOP=$(abspath ../..) \ 29 | BOARD=$(BOARD) \ 30 | BOARD_DIR=$(BOARD_DIR) \ 31 | BUILD=$(BUILD) 32 | 33 | mboot: 34 | $(Q)$(MAKE) -C $(MICROPY_TOP)/ports/stm32/mboot \ 35 | PROJECT_TOP=$(abspath ../..) \ 36 | BOARD=$(BOARD) \ 37 | BOARD_DIR=$(BOARD_DIR) \ 38 | BUILD=$(BUILD)-mboot 39 | 40 | deploy: 41 | $(PYTHON) $(MICROPY_TOP)/tools/pydfu.py -u $(BUILD)/firmware.dfu 42 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/README.md: -------------------------------------------------------------------------------- 1 | This directory contains board definition files for a customised PYBD-SF2W board. 2 | It customises it as follows: 3 | - Default SYSCLK at 96MHz 4 | - Bluetooth support not included 5 | - Relocates ISR vector table to RAM 6 | - Prints a message before running boot.py 7 | 8 | First, make sure all submodules are initialised in the micropython repository: 9 | 10 | $ git -C ../../lib/micropython submodule update --init lib/lwip lib/mbedtls lib/stm32lib 11 | 12 | Then build the firmware with: 13 | 14 | $ make 15 | 16 | To deploy the firmware to the board, make sure the board is in it's bootloader then run: 17 | 18 | $ make deploy 19 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/bdev.c: -------------------------------------------------------------------------------- 1 | // Use PYBD_SF2 bdev implementation. 2 | #include "boards/PYBD_SF2/bdev.c" 3 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/board_init.c: -------------------------------------------------------------------------------- 1 | #include "py/mphal.h" 2 | #include "boardctrl.h" 3 | 4 | // Use PYBD_SF2 implementation, but add extra initialisation. 5 | #include "boards/PYBD_SF2/board_init.c" 6 | 7 | #if !BUILDING_MBOOT 8 | 9 | void board_early_init_custom(void) { 10 | // Call PYBD_SF2's board init function. 11 | board_early_init(); 12 | 13 | // Relocate ISR vector table to ITCM RAM (can't use memcpy because dest=0). 14 | __disable_irq(); 15 | const uint32_t *src = (void*)SCB->VTOR; 16 | uint32_t *dest = RAMITCM_BASE; 17 | for (unsigned int i = 0; i < 128; ++i) { 18 | *dest++ = *src++; 19 | } 20 | SCB->VTOR = RAMITCM_BASE; 21 | __enable_irq(); 22 | } 23 | 24 | int board_run_boot_py(boardctrl_state_t *state) { 25 | mp_printf(&mp_plat_print, "Starting custom board\n"); 26 | return boardctrl_run_boot_py(state); 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/manifest.py: -------------------------------------------------------------------------------- 1 | include("$(MPY_DIR)/extmod/asyncio/manifest.py") 2 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/mpconfigboard.h: -------------------------------------------------------------------------------- 1 | // Take PYBD_SF2 as base configuration. 2 | #include "boards/PYBD_SF2/mpconfigboard.h" 3 | 4 | #undef MICROPY_HW_BOARD_NAME 5 | #undef MICROPY_HW_CLK_PLLM 6 | #undef MICROPY_HW_CLK_PLLN 7 | #undef MICROPY_HW_CLK_PLLP 8 | #undef MICROPY_HW_CLK_PLLQ 9 | #undef MICROPY_HW_FLASH_LATENCY 10 | #undef MICROPY_BOARD_EARLY_INIT 11 | 12 | #define MICROPY_HW_BOARD_NAME "Custom PYBD-SF2W" 13 | #define MICROPY_BOARD_EARLY_INIT board_early_init_custom 14 | #define MICROPY_BOARD_RUN_BOOT_PY board_run_boot_py 15 | 16 | // HSE is 25MHz, run SYS at 96MHz 17 | #define MICROPY_HW_CLK_PLLM (25) 18 | #define MICROPY_HW_CLK_PLLN (192) 19 | #define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2) 20 | #define MICROPY_HW_CLK_PLLQ (4) 21 | #define MICROPY_HW_FLASH_LATENCY (FLASH_LATENCY_3) 22 | 23 | struct _boardctrl_state_t; 24 | void board_early_init_custom(void); 25 | int board_run_boot_py(struct _boardctrl_state_t *state); 26 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/mpconfigboard.mk: -------------------------------------------------------------------------------- 1 | # MCU settings 2 | MCU_SERIES = f7 3 | CMSIS_MCU = STM32F722xx 4 | MICROPY_FLOAT_IMPL = single 5 | AF_FILE = boards/stm32f722_af.csv 6 | LD_FILES = boards/PYBD_SF2/f722_qspi.ld 7 | TEXT0_ADDR = 0x08008000 8 | TEXT1_ADDR = 0x90000000 9 | TEXT0_SECTIONS = .isr_vector .text .data .ARM 10 | TEXT1_SECTIONS = .text_ext 11 | 12 | # MicroPython settings 13 | MICROPY_PY_LWIP = 1 14 | MICROPY_PY_NETWORK_CYW43 = 1 15 | MICROPY_PY_SSL = 1 16 | MICROPY_SSL_MBEDTLS = 1 17 | MICROPY_VFS_LFS2 = 1 18 | 19 | # Custom C modules 20 | USER_C_MODULES = $(PROJECT_TOP)/src/cmodules 21 | 22 | # PYBD-specific frozen modules 23 | FROZEN_MANIFEST = $(BOARD_DIR)/manifest.py 24 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/pins.csv: -------------------------------------------------------------------------------- 1 | W3,PE3 2 | W5,PA11 3 | W5B,PI0 4 | W6,PA5 5 | W7,PA4 6 | W7B,PG7 7 | W8,PB11 8 | W9,PA12 9 | W9B,PI1 10 | W10,PA6 11 | W11,PA3 12 | W12,PB10 13 | W14,PA7 14 | W15,PA2 15 | W16,PA15 16 | W17,PA1 17 | W18,PD3 18 | W19,PA0 19 | W20,PD0 20 | W21,PC14 21 | W22,PE12 22 | W22B,PF6 23 | W22C,PF7 24 | W22D,PF10 25 | W23,PE1 26 | W24,PC1 27 | W25,PE0 28 | W26,PC13 29 | W27,PA8 30 | W27B,PC6 31 | W28,PE6 32 | W29,PB3 33 | W30,PE5 34 | W32,PE4 35 | W33,PH2 36 | W34,PH3 37 | W43,PB0 38 | W43B,PC0 39 | W43C,PF9 40 | W43D,PF11 41 | W45,PB12 42 | W45B,PE14 43 | W45C,PH8 44 | W46,PB5 45 | W47,PC5 46 | W47B,PF14 47 | W49,PB13 48 | W50,PB4 49 | W51,PC4 50 | W51B,PF15 51 | W52,PA10 52 | W53,PC2 53 | W53B,PF8 54 | W54,PA9 55 | W55,PB9 56 | W56,PA14 57 | W57,PC3 58 | W57B,PF13 59 | W58,PG11 60 | W59,PB8 61 | W60,PG12 62 | W61,PB7 63 | W62,PD7 64 | W63,PB1 65 | W63B,PD9 66 | W64,PD6 67 | W65,PD8 68 | W66,PG9 69 | W67,PA13 70 | W68,PG10 71 | W70,PB14 72 | W71,PE15 73 | W72,PB15 74 | W73,PH6 75 | W74,PH7 76 | X1,PA0 77 | X2,PA1 78 | X3,PA2 79 | X4,PA3 80 | X5,PA4 81 | X5B,PG7 82 | X6,PA5 83 | X7,PA6 84 | X8,PA7 85 | X9,PB8 86 | X10,PB9 87 | X11,PC4 88 | X11B,PF15 89 | X12,PC5 90 | X12B,PF14 91 | Y1,PA9 92 | Y2,PA10 93 | Y3,PB4 94 | Y4,PB5 95 | Y5,PB12 96 | Y5B,PE14 97 | Y5C,PH8 98 | Y6,PB13 99 | Y7,PC2 100 | Y7B,PF8 101 | Y8,PC3 102 | Y8B,PF13 103 | Y9,PB10 104 | Y10,PB11 105 | Y11,PB0 106 | Y11B,PC0 107 | Y11C,PF9 108 | Y11D,PF11 109 | Y12,PB1 110 | Y12B,PD9 111 | EN_3V3,PF2 112 | PULL_SCL,PF1 113 | PULL_SDA,PH5 114 | LED_RED,PF3 115 | LED_GREEN,PF4 116 | LED_BLUE,PF5 117 | USR,PA13 118 | USB_DM,PA11 119 | USB_DP,PA12 120 | USB_HS_DM,PB14 121 | USB_HS_DP,PB15 122 | -QSPI1_CS,PE13 123 | -QSPI1_CLK,PE11 124 | -QSPI1_D0,PE7 125 | -QSPI1_D1,PE8 126 | -QSPI1_D2,PE9 127 | -QSPI1_D3,PE10 128 | -QSPI2_CS,PB6 129 | -QSPI2_CLK,PB2 130 | -QSPI2_D0,PD11 131 | -QSPI2_D1,PD12 132 | -QSPI2_D2,PE2 133 | -QSPI2_D3,PD13 134 | -SD_D0,PG9 135 | -SD_D1,PG10 136 | -SD_D2,PG11 137 | -SD_D3,PG12 138 | -SD_CMD,PD7 139 | -SD_CK,PD6 140 | SD_SW,PA14 141 | -WL_REG_ON,PD4 142 | -WL_HOST_WAKE,PI8 143 | -WL_RFSW_VDD,PI9 144 | -WL_GPIO_1,PI11 145 | -WL_GPIO_2,PI7 146 | -WL_GPIO_4,PI9 147 | -WL_SDIO_0,PC8 148 | -WL_SDIO_1,PC9 149 | -WL_SDIO_2,PC10 150 | -WL_SDIO_3,PC11 151 | -WL_SDIO_CMD,PD2 152 | -WL_SDIO_CLK,PC12 153 | -BT_RXD,PC7 154 | -BT_TXD,PG14 155 | -BT_CTS,PG13 156 | -BT_RTS,PG8 157 | -BT_GPIO_3,PG15 158 | -BT_GPIO_4,PI5 159 | -BT_GPIO_5,PI4 160 | -BT_PCM_SYNC,PE4 161 | -BT_PCM_IN,PE6 162 | -BT_PCM_OUT,PE3 163 | -BT_PCM_CLK,PE5 164 | -BT_I2C_D0,PI10 165 | -BT_REG_ON,PI6 166 | -BT_HOST_WAKE,PD10 167 | -BT_DEV_WAKE,PD5 168 | ,PD1 169 | ,PD14 170 | ,PD15 171 | ,PF0 172 | ,PF12 173 | ,PG0 174 | ,PG1 175 | ,PG2 176 | ,PG3 177 | ,PG4 178 | ,PG5 179 | ,PG6 180 | ,PH4 181 | ,PH9 182 | ,PH10 183 | ,PH11 184 | ,PH12 185 | ,PH13 186 | ,PH14 187 | ,PH15 188 | ,PI2 189 | ,PI3 190 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBD_SF2/stm32f7xx_hal_conf.h: -------------------------------------------------------------------------------- 1 | // Use PYBD_SF2 configuration. 2 | #include "boards/PYBD_SF2/stm32f7xx_hal_conf.h" 3 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for custom MicroPython stm32 board. 2 | 3 | ################################################################################ 4 | # Define your settings here. 5 | 6 | # The board name. 7 | BOARD ?= CUSTOM_PYBV11 8 | 9 | # Location of MicroPython repository. 10 | MICROPY_TOP ?= $(abspath ../../lib/micropython) 11 | 12 | ################################################################################ 13 | # Define your targets here. 14 | 15 | all: firmware 16 | 17 | ################################################################################ 18 | # Items below this line do not generally need to be changed. 19 | 20 | BOARD_DIR = $(abspath .) 21 | BUILD = $(abspath build) 22 | 23 | include $(MICROPY_TOP)/py/mkenv.mk 24 | include $(MICROPY_TOP)/py/mkrules.mk 25 | 26 | firmware: 27 | $(Q)$(MAKE) -C $(MICROPY_TOP)/ports/stm32 \ 28 | PROJECT_TOP=$(abspath ../..) \ 29 | BOARD=$(BOARD) \ 30 | BOARD_DIR=$(BOARD_DIR) \ 31 | BUILD=$(BUILD) 32 | 33 | deploy: 34 | $(PYTHON) $(MICROPY_TOP)/tools/pydfu.py -u $(BUILD)/firmware.dfu 35 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/README.md: -------------------------------------------------------------------------------- 1 | This directory contains board definition files for an stm32-based board, which 2 | is based on the PYBV11 board definition. 3 | 4 | To build it: 5 | 6 | $ make 7 | 8 | To deploy the firmware to the board: 9 | 10 | $ make deploy 11 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/board_init.c: -------------------------------------------------------------------------------- 1 | #include "py/mphal.h" 2 | #include "boardctrl.h" 3 | 4 | int board_run_boot_py(boardctrl_state_t *state) { 5 | mp_printf(&mp_plat_print, "Starting custom board\n"); 6 | return boardctrl_run_boot_py(state); 7 | } 8 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/manifest.py: -------------------------------------------------------------------------------- 1 | freeze("$(BOARD_DIR)/../../src/utils", "xxd.py") 2 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/mpconfigboard.h: -------------------------------------------------------------------------------- 1 | // Take PYBV11 as base configuration. 2 | #include "ports/stm32/boards/PYBV11/mpconfigboard.h" 3 | 4 | #undef MICROPY_HW_BOARD_NAME 5 | #undef MICROPY_HW_CLK_PLLM 6 | #undef MICROPY_HW_CLK_PLLN 7 | #undef MICROPY_HW_CLK_PLLP 8 | #undef MICROPY_HW_CLK_PLLQ 9 | #undef MICROPY_HW_FLASH_LATENCY 10 | 11 | #define MICROPY_HW_BOARD_NAME "Custom PYBv1.1" 12 | #define MICROPY_BOARD_RUN_BOOT_PY board_run_boot_py 13 | 14 | // HSE is 12MHz, run SYS at 48MHz 15 | #define MICROPY_HW_CLK_PLLM (12) 16 | #define MICROPY_HW_CLK_PLLN (192) 17 | #define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV4) 18 | #define MICROPY_HW_CLK_PLLQ (4) 19 | #define MICROPY_HW_FLASH_LATENCY (FLASH_LATENCY_1) 20 | 21 | struct _boardctrl_state_t; 22 | int board_run_boot_py(struct _boardctrl_state_t *state); 23 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/mpconfigboard.mk: -------------------------------------------------------------------------------- 1 | # MCU settings 2 | MCU_SERIES = f4 3 | CMSIS_MCU = STM32F405xx 4 | AF_FILE = boards/stm32f405_af.csv 5 | LD_FILES = boards/stm32f405.ld boards/common_ifs.ld 6 | TEXT0_ADDR = 0x08000000 7 | TEXT1_ADDR = 0x08020000 8 | 9 | # MicroPython settings 10 | MICROPY_FLOAT_IMPL = double 11 | MICROPY_VFS_LFS1 = 1 12 | MICROPY_VFS_LFS2 = 1 13 | 14 | # Custom C modules 15 | USER_C_MODULES = $(PROJECT_TOP)/src/cmodules 16 | 17 | # PYBD-specific frozen modules 18 | FROZEN_MANIFEST = $(BOARD_DIR)/manifest.py 19 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/pins.csv: -------------------------------------------------------------------------------- 1 | X1,PA0 2 | X2,PA1 3 | X3,PA2 4 | X4,PA3 5 | X5,PA4 6 | X6,PA5 7 | X7,PA6 8 | X8,PA7 9 | X9,PB6 10 | X10,PB7 11 | X11,PC4 12 | X12,PC5 13 | X13,Reset 14 | X14,GND 15 | X15,3.3V 16 | X16,VIN 17 | X17,PB3 18 | X18,PC13 19 | X19,PC0 20 | X20,PC1 21 | X21,PC2 22 | X22,PC3 23 | X23,A3.3V 24 | X24,AGND 25 | Y1,PC6 26 | Y2,PC7 27 | Y3,PB8 28 | Y4,PB9 29 | Y5,PB12 30 | Y6,PB13 31 | Y7,PB14 32 | Y8,PB15 33 | Y9,PB10 34 | Y10,PB11 35 | Y11,PB0 36 | Y12,PB1 37 | Y13,Reset 38 | Y14,GND 39 | Y15,3.3V 40 | Y16,VIN 41 | SW,PB3 42 | LED_RED,PA13 43 | LED_GREEN,PA14 44 | LED_YELLOW,PA15 45 | LED_BLUE,PB4 46 | MMA_INT,PB2 47 | MMA_AVDD,PB5 48 | SD_D0,PC8 49 | SD_D1,PC9 50 | SD_D2,PC10 51 | SD_D3,PC11 52 | SD_CMD,PD2 53 | SD_CK,PC12 54 | SD,PA8 55 | SD_SW,PA8 56 | USB_VBUS,PA9 57 | USB_ID,PA10 58 | USB_DM,PA11 59 | USB_DP,PA12 60 | -------------------------------------------------------------------------------- /boards/CUSTOM_PYBV11/stm32f4xx_hal_conf.h: -------------------------------------------------------------------------------- 1 | // Use PYBV11 configuration. 2 | #include "boards/PYBV11/stm32f4xx_hal_conf.h" 3 | -------------------------------------------------------------------------------- /boards/README.md: -------------------------------------------------------------------------------- 1 | This directory contains all boards that can be built as part of this project. 2 | -------------------------------------------------------------------------------- /src/README.md: -------------------------------------------------------------------------------- 1 | In this directory are the custom modules that can be built into the firmware. 2 | There are examples of modules written in Python and C. 3 | -------------------------------------------------------------------------------- /src/cmodules/cexample/micropython.cmake: -------------------------------------------------------------------------------- 1 | # Create an INTERFACE library for our C module. 2 | add_library(usermod_cexample INTERFACE) 3 | 4 | # Add our source files to the lib 5 | target_sources(usermod_cexample INTERFACE 6 | ${CMAKE_CURRENT_LIST_DIR}/modcexample.c 7 | ) 8 | 9 | # Add the current directory as an include directory. 10 | target_include_directories(usermod_cexample INTERFACE 11 | ${CMAKE_CURRENT_LIST_DIR} 12 | ) 13 | 14 | # Link our INTERFACE library to the usermod target. 15 | target_link_libraries(usermod INTERFACE usermod_cexample) 16 | -------------------------------------------------------------------------------- /src/cmodules/cexample/micropython.mk: -------------------------------------------------------------------------------- 1 | # Add all C files to SRC_USERMOD. 2 | SRC_USERMOD += $(USERMOD_DIR)/modcexample.c 3 | -------------------------------------------------------------------------------- /src/cmodules/cexample/modcexample.c: -------------------------------------------------------------------------------- 1 | // Include MicroPython API. 2 | #include "py/runtime.h" 3 | 4 | // This is the function which will be called from Python as cexample.add_ints(a, b). 5 | STATIC mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) { 6 | // Extract the ints from the micropython input objects. 7 | int a = mp_obj_get_int(a_obj); 8 | int b = mp_obj_get_int(b_obj); 9 | 10 | // Calculate the addition and convert to MicroPython object. 11 | return mp_obj_new_int(a + b); 12 | } 13 | // Define a Python reference to the function above. 14 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints); 15 | 16 | // Define all properties of the module. 17 | // Table entries are key/value pairs of the attribute name (a string) 18 | // and the MicroPython object reference. 19 | // All identifiers and strings are written as MP_QSTR_xxx and will be 20 | // optimized to word-sized integers by the build system (interned strings). 21 | STATIC const mp_rom_map_elem_t example_module_globals_table[] = { 22 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample) }, 23 | { MP_ROM_QSTR(MP_QSTR_add_ints), MP_ROM_PTR(&example_add_ints_obj) }, 24 | }; 25 | STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table); 26 | 27 | // Define module object. 28 | const mp_obj_module_t example_user_cmodule = { 29 | .base = { &mp_type_module }, 30 | .globals = (mp_obj_dict_t *)&example_module_globals, 31 | }; 32 | 33 | // Register the module to make it available in Python. 34 | MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule); 35 | -------------------------------------------------------------------------------- /src/cmodules/cexample2/micropython.cmake: -------------------------------------------------------------------------------- 1 | # Create an INTERFACE library for our C module. 2 | add_library(usermod_cexample2 INTERFACE) 3 | 4 | # Add our source files to the lib 5 | target_sources(usermod_cexample2 INTERFACE 6 | ${CMAKE_CURRENT_LIST_DIR}/modcexample2.c 7 | ) 8 | 9 | # Add the current directory as an include directory. 10 | target_include_directories(usermod_cexample2 INTERFACE 11 | ${CMAKE_CURRENT_LIST_DIR} 12 | ) 13 | 14 | # Link our INTERFACE library to the usermod target. 15 | target_link_libraries(usermod INTERFACE usermod_cexample2) 16 | -------------------------------------------------------------------------------- /src/cmodules/cexample2/micropython.mk: -------------------------------------------------------------------------------- 1 | # Add all C files to SRC_USERMOD. 2 | SRC_USERMOD += $(USERMOD_DIR)/modcexample2.c 3 | -------------------------------------------------------------------------------- /src/cmodules/cexample2/modcexample2.c: -------------------------------------------------------------------------------- 1 | #include "py/runtime.h" 2 | 3 | STATIC mp_obj_t example_sub_ints(mp_obj_t a_obj, mp_obj_t b_obj) { 4 | int a = mp_obj_get_int(a_obj); 5 | int b = mp_obj_get_int(b_obj); 6 | return mp_obj_new_int(a - b); 7 | } 8 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_sub_ints_obj, example_sub_ints); 9 | 10 | STATIC const mp_rom_map_elem_t example_module_globals_table[] = { 11 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample2) }, 12 | { MP_ROM_QSTR(MP_QSTR_sub_ints), MP_ROM_PTR(&example_sub_ints_obj) }, 13 | }; 14 | STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table); 15 | 16 | const mp_obj_module_t example_user_cmodule2 = { 17 | .base = { &mp_type_module }, 18 | .globals = (mp_obj_dict_t *)&example_module_globals, 19 | }; 20 | 21 | MP_REGISTER_MODULE(MP_QSTR_cexample2, example_user_cmodule2); 22 | -------------------------------------------------------------------------------- /src/utils/xxd.py: -------------------------------------------------------------------------------- 1 | from machine import mem8, mem16 2 | 3 | 4 | def xxd(addr, num_bytes=64, width=16): 5 | for i in range((num_bytes + width - 1) // width): 6 | print("%08x:" % (addr + i * width), end="") 7 | for j in range(0, width, 2): 8 | word = mem16[addr + i * width + j] 9 | print(" %02x%02x" % (word & 0xFF, word >> 8), end="") 10 | print(" ", end="") 11 | for j in range(width): 12 | byte = mem8[addr + i * width + j] 13 | if 32 <= byte <= 126: 14 | print(chr(byte), end="") 15 | else: 16 | print(".", end="") 17 | print() 18 | --------------------------------------------------------------------------------